You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

本地正常的Curl API调用在服务器特定目录报错问题排查

PHP Curl在服务器特定目录下报TCP连接重置错误

本地运行以下PHP Curl代码可正常连接API并获取数据,但上传到服务器后,放在example.test.com目录下运行时触发Curl error: 56 TCP connection reset by peer错误,而放在同服务器的example.portal.com目录下的同类脚本则运行正常。

代码示例

$finalData  = null;
$url        = trim('https://example.portal.com/staging/api/v1/jobs');
$headers    = array(
                    "Accept:*/*",
                    "xsrf-token:eyJpdiI6IndYd2hkNkJBWGF1OStmRUc4bnllbUE9PSIsInZhbHVlIjoiYk5jdUh1a1lxZTFqK0RPTUxVcnU1UT09IiwibWFjIjoiNGQ5MmVmNjNiOTRjNzkxYjc2MGVkZDRhMWExOTFkOGZmMTE3OWFlZDkxODYyZjk2NDQzOGZhNGZiODcyYWZjNCJ9"
                );
$postType   = 'CURL_POST_DATA_TYPE_X_WWW_FORM_URLENCODE';
$postData   = array(
                'auth_token'    => '30bb573d6a09d2eee2a564d51742252c',
                'version'       => 1,
                'sort_by'       => 'j.title',
                'sort'          => 'desc'
                );
if($postType == 'CURL_POST_DATA_TYPE_X_WWW_FORM_URLENCODE')
{
    $dataString     = '';
    foreach($postData as $keyData => $rowData)
    {
        $dataString .= rawurlencode($keyData) . '=' . rawurlencode($rowData) . '&';
    }
    $postData = trim($dataString, "&");
    if(empty($headers) ||  $headers === null)
    {
        $headers    = array("Content-type:application/x-www-form-urlencoded");
    }
    else
    {
        $headers[]  = "Content-type:application/x-www-form-urlencoded";
    }
}
//$headers[]  = "Content-type:application/json";
$ch             = curl_init();
curl_setopt($ch, CURLOPT_URL, trim($url));
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, TRUE); // remove body
//curl_setopt($ch, CURLOPT_NOBODY, TRUE); // remove body
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 260);
curl_setopt($ch, CURLOPT_TIMEOUT, 260);
$output         = curl_exec($ch);
$httpCode       = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if(curl_errno($ch))
{
    echo 'Curl error: ' . curl_errno($ch) . ' ' . curl_error($ch);
}

curl_close($ch);
if($output !== false)
{
    $finalData  = json_decode($output, true);
}
else
{
    echo "False";
}
//echo $httpCode;
print_r($finalData);

可能的原因

  • 虚拟主机出站权限限制:服务器上不同虚拟主机(example.portal.com和example.test.com)可能配置了不同的防火墙、SELinux或安全组规则,example.test.com的对外出站请求被拦截,导致TCP连接被重置。
  • API服务器域名白名单限制:目标API服务器可能仅允许example.portal.com域名发起的请求,当example.test.com作为请求来源时,不在白名单内,API服务器主动重置连接。
  • PHP环境配置差异:两个目录对应的PHP环境可能存在配置差异,比如example.test.com的PHP禁用了Curl的某些功能、使用了不兼容的Curl版本或SSL库,导致握手过程异常被服务器重置。
  • 网络路由/代理差异:服务器上不同虚拟主机可能使用不同的网络路由或代理设置,example.test.com的请求路径无法正常到达API服务器,中途被网络节点重置连接。
  • 令牌与域名绑定:代码中的xsrf-token或auth_token可能与example.portal.com域名绑定,当从example.test.com发起请求时,令牌失效,API服务器拒绝连接并重置TCP链接。

内容的提问来源于stack exchange,提问作者Saswat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 08:05:43