feat: Authorization 回调原文(含GET参数)转发到测试环境

This commit is contained in:
liufumingren 2026-09-21 17:03:38 +08:00
parent ca30f3302b
commit ca919e4a48

View File

@ -99,6 +99,9 @@ class Index extends Frontend
// 获取原始POST数据
$from_xml = file_get_contents('php://input');
// 原文(含GET参数)转发一份到测试环境
$this->forwardToTest('/Authorization', $from_xml);
// 记录日志用于调试
\think\Log::info('微信回调参数: ' . json_encode([
'signature' => $signature,
@ -654,6 +657,51 @@ class Index extends Frontend
}
}
/**
* 转发原始请求到测试环境
* @param string $path 目标路径
* @param string $rawBody 原始报文(POST body)
* @param string $host 目标域名
* @return bool
*/
private function forwardToTest($path, $rawBody = '', $host = 'http://test.wms.gztbyc.com')
{
try {
// 拼装目标地址:域名 + 路径 + 原始GET参数
$url = rtrim($host, '/') . '/' . ltrim($path, '/');
$query = $this->request->get();
if (!empty($query)) {
$url .= (stripos($url, '?') !== false ? '&' : '?') . http_build_query($query);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $rawBody);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Expect:', 'Content-Type: text/xml']);
$response = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
if ($error) {
\think\Log::error('转发到测试环境失败: ' . $url . ' 错误: ' . $error);
return false;
}
\think\Log::info('转发到测试环境成功: ' . $url . ' 返回: ' . $response);
return true;
} catch (\Exception $e) {
\think\Log::error('转发到测试环境异常: ' . $e->getMessage());
return false;
}
}
/**
* 修复XML编码问题
*/