PHP调用BigCommerce API遇CURL超时但接口操作成功求助
排查PHP CURL调用BigCommerce API上传后超时问题
问题详情
此前数周PHP CURL调用BigCommerce API均正常,近几日频繁触发Operation timed out after 60001 milliseconds with 0 bytes received错误。但BigCommerce端已成功完成产品创建操作,CURLOPT_VERBOSE日志显示在输出We are completely uploaded and fine后立即超时,怀疑问题出在本地环境而非远程平台。
触发CURL调用的PHP代码
$this->addHeader('Content-Type', $this->getContentType()); $this->addHeader('Connection', 'close'); if (!is_string($body)) { $body = json_encode($body); } $this->initializeRequest(); curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($this->curl, CURLOPT_URL, $url); curl_setopt($this->curl, CURLOPT_POST, true); curl_setopt($this->curl, CURLOPT_PUT, false); curl_setopt($this->curl, CURLOPT_HTTPGET, false); curl_setopt($this->curl, CURLOPT_POSTFIELDS, $body); curl_exec($this->curl); return $this->handleResponse();
CURLOPT_VERBOSE日志
* Hostname api.bigcommerce.com was found in DNS cache * Trying 34.96.84.189... * TCP_NODELAY set * Connected to api.bigcommerce.com (34.96.84.189) port 443 (#0) * ALPN, offering http/1.1 * successfully set certificate verify locations: * CAfile: /etc/pki/tls/certs/ca-bundle.crt CApath: none * SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384 * ALPN, server accepted to use http/1.1 * Server certificate: * subject: C=US; ST=Texas; L=Austin; O=BigCommerce, Inc.; CN=*.bigcommerce.com * start date: Mar 16 00:00:00 2022 GMT * expire date: Mar 7 23:59:59 2023 GMT * subjectAltName: host "api.bigcommerce.com" matched cert's "*.bigcommerce.com" * issuer: C=US; O=DigiCert Inc; CN=DigiCert TLS RSA SHA256 2020 CA1 * SSL certificate verify ok. > POST /stores/[sensitive-data-trimmed-out]/v3/catalog/products HTTP/1.1 Host: api.bigcommerce.com Accept-Encoding: deflate, gzip, br X-Auth-Client: [sensitive-data-trimmed-out] X-Auth-Token: [sensitive-data-trimmed-out] Content-Type: application/json Connection: close Accept: application/json Content-Length: 4009 Expect: 100-continue < HTTP/1.1 100 Continue * We are completely uploaded and fine * Operation timed out after 60001 milliseconds with 0 bytes received * Closing connection 0 [2022-09-08 05:29:33] [ERROR] Message: M\BcBundle\VendorExtends\Bigcommerce\Api\NetworkError: Operation timed out after 60001 milliseconds with 0 bytes received (uncaught exception) at Connection.php line 280 while running console command `m:bc:export:process` File: ConsoleExceptionListener.php Line: 37 Class: M\PaymentsBundle\EventListener\ConsoleExceptionListener Function: onConsoleException[2022-09-08 05:29:33] [ERROR] Message: Error thrown while running command "m:bc:export:process 163 1301 1950". Message: "Operation timed out after 60001 milliseconds with 0 bytes received" File: ErrorListener.php Line: 48 Class: Symfony\Component\Console\EventListener\ErrorListener Function: onConsoleError In Connection.php line 280: Operation timed out after 60001 milliseconds with 0 bytes received
排查思路
拆分超时参数,区分连接与读取阶段
当前超时为统一的60秒,建议分别设置连接超时和整体请求超时,临时调高读取超时值验证是否为响应返回延迟导致:curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, 10); // 连接阶段超时10秒 curl_setopt($this->curl, CURLOPT_TIMEOUT, 120); // 整个请求周期超时120秒检查本地TCP连接状态与防火墙规则
发送完请求却收不到响应,可能是本地网络设备提前切断连接或TIME_WAIT连接堆积:- 执行
netstat -an | grep TIME_WAIT查看连接状态,若数量过多,调整TCP参数(如net.ipv4.tcp_tw_reuse、net.ipv4.tcp_tw_recycle) - 排查本地防火墙、安全组是否存在拦截出站响应的规则
- 执行
禁用
Expect: 100-continue头部
日志显示发送了该头部,部分场景下会增加交互延迟,可通过CURL禁用:curl_setopt($this->curl, CURLOPT_HTTPHEADER, array('Expect:'));若已有自定义头部,需将该配置合并到现有头部数组中。
验证CURL版本与SSL兼容性
日志显示使用TLSv1.3,旧版CURL可能存在兼容性问题:- 执行
curl --version查看本地CURL版本 - 强制使用TLSv1.2测试:
curl_setopt($this->curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
- 执行
优化响应处理逻辑
在curl_exec后立即检查CURL错误,避免误判:$response = curl_exec($this->curl); if(curl_errno($this->curl)) { $errorMsg = curl_error($this->curl); // 记录详细错误信息,区分真超时与其他异常 }同时检查
handleResponse方法是否存在阻塞操作。排查DNS缓存问题
手动指定日志中的BigCommerce API IP(34.96.84.189)测试,或刷新本地DNS缓存,验证是否为DNS解析不稳定导致。
内容的提问来源于stack exchange,提问作者kamil_de
相关产品推荐
相关产品推荐

