PHP中CURL请求头未生效且请求超时问题求助
PHP CURL请求超时排查求助
问题描述
在PHP中发送CURL请求,已配置请求头,但curl_getinfo()返回无请求头数据。该请求在服务器及命令行curl中均超时,本地桌面端发送相同请求可正常获取结果。浏览器访问目标站点时会出现Bitdefender证书不匹配警告,推测可能是防火墙或网络层面问题,附上相关信息寻求解决思路。
相关代码
public static function GetCurlHandler() { // Initialize CURL handler $curlHandler = curl_init(); curl_setopt( $curlHandler, CURLOPT_COOKIEJAR, PATH_TO_COOKIES_FILE ); curl_setopt( $curlHandler, CURLOPT_COOKIEFILE, PATH_TO_COOKIES_FILE ); curl_setopt( $curlHandler, CURLOPT_CAINFO, PATH_TO_CACERT_PEM ); curl_setopt( $curlHandler, CURLOPT_SSL_VERIFYPEER, 0 ); curl_setopt( $curlHandler, CURLOPT_SSL_VERIFYHOST, 2 ); curl_setopt( $curlHandler, CURLOPT_RETURNTRANSFER, true ); curl_setopt( $curlHandler, CURLOPT_CUSTOMREQUEST, 'GET' ); curl_setopt( $curlHandler, CURLOPT_ENCODING, 'gzip, deflate' ); curl_setopt( $curlHandler, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 ); // default to older CURL version for supporting older programs $headers = array(); $headers[] = 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0'; $headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'; $headers[] = 'Accept-Language: en-US,en;q=0.5'; $headers[] = 'Connection: keep-alive'; $headers[] = 'Upgrade: h2'; // tell the server to use https2 if available curl_setopt( $curlHandler, CURLOPT_HTTPHEADER, $headers ); curl_setopt($curlHandler, CURLOPT_HEADER, TRUE); curl_setopt( $curlHandler, CURLINFO_HEADER_OUT, true ); return $curlHandler; } static function GetCapabilities($urlBase = 'https://madison.rexburg.org/mrgis/rest/services/Downtown/BLDGFootprint/FeatureServer/0',$recursionDepth=1) { $url = $urlBase.'?f=json'; Log::info("Getting capabilities for $url"); $curlHandler = self::GetCurlHandler(); curl_setopt($curlHandler, CURLOPT_URL, $url); $curlResponse = curl_exec( $curlHandler ); $info = curl_getinfo( $curlHandler ); print_r( $info ); print_r( curl_error( $curlHandler ) ); var_dump( $curlResponse ); echo "\n"; curl_close( $curlHandler ); $jsonObject = json_decode($curlResponse); if( empty( $jsonObject ) ) { \log::error( 'empty response from server, retrying with other HTTP version...' ); $curlHandler = self::GetCurlHandler(); curl_setopt($curlHandler, CURLOPT_URL, $url); curl_setopt($curlHandler, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_NONE); $curlResponse = curl_exec( $curlHandler ); print_r( curl_error( $curlHandler ) ); curl_close( $curlHandler ); $jsonObject = json_decode( $curlResponse ); if( empty( $jsonObject ) ) { var_dump( $curlResponse ); throw new \Exception( 'empty response from server' ); // This is where I end up } }
执行结果
‣ 00:00:00.049 [INFO] Getting capabilities for https://madison.rexburg.org/mrgis/rest/services/Downtown/BLDGFootprint/FeatureServer/0?f=json Array ( [url] => https://madison.rexburg.org/mrgis/rest/services/Downtown/BLDGFootprint/FeatureServer/0?f=json [content_type] => [http_code] => 0 [header_size] => 0 [request_size] => 0 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 21.220237 [namelookup_time] => 0.002064 [connect_time] => 0 [pretransfer_time] => 0 [size_upload] => 0 [size_download] => 0 [speed_download] => 0 [speed_upload] => 0 [download_content_length] => -1 [upload_content_length] => -1 [starttransfer_time] => 0 [redirect_time] => 0 [redirect_url] => [primary_ip] => [certinfo] => Array ( ) [primary_port] => 0 [local_ip] => [local_port] => 0 [http_version] => 0 [protocol] => 0 [ssl_verifyresult] => 0 [scheme] => [appconnect_time_us] => 0 [connect_time_us] => 0 [namelookup_time_us] => 2064 [pretransfer_time_us] => 0 [redirect_time_us] => 0 [starttransfer_time_us] => 0 [total_time_us] => 21220237 ) Failed to connect to madison.rexburg.org port 443: Timed outbool(false) ‣ 00:00:21.281 [ERROR] empty response from server, retrying with other HTTP version... Failed to connect to madison.rexburg.org port 443: Timed outbool(false) ‣ 00:00:42.513 [ERROR] Exception: empty response from server
等效命令行
curl -L "https://madison.rexburg.org/mrgis/rest/services/Downtown/BLDGFootprint/FeatureServer/0?f=json" -H "Accept-Language: en-US,en;q=0.5" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0" -H "Accept-Encoding: gzip, deflate" -H "Upgrade: h2" --output - --verbose --http1.1
服务器信息
- PHP 7.4.33
- Windows NT 10.0 build 17763 (Windows Server 2016) AMD64
- 运行于Google Cloud Services VM
- 编译环境:Visual C++ 2017
- Apache 2.0 Handler
- Configure命令:
cscript /nologo /e:jscript configure.js "--enable-snapshot-build" "--enable-debug-pack" "--with-pdo-oci=c:\php-snap-build\deps_aux\oracle\x64\instantclient_12_1\sdk,shared" "--with-oci8-12c=c:\php-snap-build\deps_aux\oracle\x64\instantclient_12_1\sdk,shared" "--enable-object-out-dir=../obj/" "--enable-com-dotnet=shared" "--without-analyzer" "--with-pgo" - CURL版本:8.0.1 (Windows) libcurl/8.0.1
相关基础设施信息
- 默认路由:
{ "creationTimestamp": "2017-12-08T13:34:07.643-08:00", "description": "Default route to the Internet.", "destRange": "0.0.0.0/0", "id": "4955173435988163488", "kind": "compute#route", "name": "default-route-b06db13fc2a2a102", "network": "projects/our-server/global/networks/default", "nextHopGateway": "projects/our-server/global/gateways/default-internet-gateway", "priority": 1000, "selfLink": "projects/our-server/global/routes/default-route-b06db13fc2a2a102" }
- VPC防火墙信息:HTTP流量关闭,HTTPS流量开启,无规则阻止相关端口出站
解决思路
网络连通性底层验证
- 在GCP VM上执行
Test-NetConnection madison.rexburg.org -Port 443(Windows),确认TCP连接能否建立。如果连不上,直接定位到网络层面阻断,和HTTP头、SSL配置无关。 - 检查VM本地Windows防火墙,确认是否允许443端口出站;同时核对GCP VPC出站规则,排查是否有高优先级规则拦截流量。
- 在GCP VM上执行
Bitdefender证书关联排查
- 目标站点证书被Bitdefender替换,可能目标服务器存在IP段拦截策略(比如拒绝云服务商IP)。可尝试在VM上通过本地桌面代理发起请求,验证是否是IP被拉黑。
- 暂时关闭VM上的Bitdefender防护组件,测试请求是否恢复正常。
CURL参数调试优化
- 移除
Upgrade: h2请求头,部分服务器对HTTP/2升级请求处理异常,强制使用HTTP/1.1测试。 - 添加超时参数:
curl_setopt($curlHandler, CURLOPT_CONNECTTIMEOUT, 10);curl_setopt($curlHandler, CURLOPT_TIMEOUT, 15);,避免无效等待。 - 开启CURL verbose日志:
curl_setopt($curlHandler, CURLOPT_VERBOSE, true);,将日志输出到文件,查看连接过程中的具体错误细节。
- 移除
DNS解析一致性验证
- 在VM上执行
nslookup madison.rexburg.org,对比本地桌面端的解析结果。如果IP不一致,尝试更换VM的DNS服务器(如8.8.8.8)测试。
- 在VM上执行
内容的提问来源于stack exchange,提问作者SlamJammington
相关产品推荐
相关产品推荐

