This commit is contained in:
liufumingren 2026-09-21 22:16:37 +08:00
parent a1b5c356ea
commit 6f4b2c46d3

View File

@ -686,47 +686,61 @@ class Index extends Frontend
}
/**
* 转发原始请求到测试环境
* @param string $path 目标路径
* @param string $rawBody 原始报文(POST body)
* @param string $host 目标域名
* @return bool
* 转发原始请求到多个环境(默认:qimen + 测试环境)
* @param string $path 目标路径
* @param string $rawBody 原始报文(POST body)
* @param array|string $hosts 目标域名(可传单个字符串)
* @return array 每个目标地址的转发结果
*/
private function forwardToTest($path, $rawBody = '', $host = 'http://qimen.wms.gztbyc.com')
private function forwardToTest($path, $rawBody = '', $hosts = [
'http://qimen.wms.gztbyc.com',
'http://test_wms.o.gztbyc.com',
])
{
try {
// 拼装目标地址:域名 + 路径 + 原始GET参数
if (is_string($hosts)) {
$hosts = [$hosts];
}
// 原始GET参数(所有环境共用)
$query = $this->request->get();
$results = [];
foreach ($hosts as $host) {
$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;
try {
$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);
$results[$url] = false;
continue;
}
\think\Log::info('转发成功: ' . $url . ' 返回: ' . $response);
$results[$url] = true;
} catch (\Exception $e) {
\think\Log::error('转发异常: ' . $url . ' 错误: ' . $e->getMessage());
$results[$url] = false;
}
\think\Log::info('转发到测试环境成功: ' . $url . ' 返回: ' . $response);
return true;
} catch (\Exception $e) {
\think\Log::error('转发到测试环境异常: ' . $e->getMessage());
return false;
}
return $results;
}