CURL(PHP)-无论调用是否成功,操作都超时

6qfn3psc  于 2022-11-13  发布在  PHP
关注(0)|答案(1)|浏览(248)

我正面临着与CURL(从PHP级别触发)和BigCommerce API相关的相当奇怪的问题。
几个星期以来,我的电话工作没有任何问题,但因为几天我遇到“操作超时后60001毫秒与0字节接收”错误.
奇怪的是,BigCommerce端的API操作是成功的-这是一个产品创建调用,产品成功创建。
在CURLOPT_VERBOSE信息中,我可以看到调用在“We are completely uploaded and fine”(我们已完全上传,一切正常)后立即失败,这表明问题出在我这边,而不是处理API调用的远程平台上。
下面是触发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

任何提示都是非常受欢迎的。

zdwk9cvp

zdwk9cvp1#

我认为您可以尝试在curl和php中设置超时。
要告诉Curl在传输仍处于活动状态时永不超时,需要将CURLOPT_TIMEOUT设置为0。

curl_setopt($ch, CURLOPT_TIMEOUT, 0);

在PHP中,同样,你必须删除时间限制,否则PHP本身(默认为30秒后)会沿着Curl的请求终止脚本。这本身就应该解决你的问题。另外,如果你需要数据完整性,你可以通过使用ignore_user_abort来增加一层安全性:

最大执行时间,以秒为单位。如果设置为零,则没有时间限制。

set_time_limit(0);

确保在客户端断开连接时保持脚本处于活动状态。

ignore_user_abort(true);

客户端断开连接将中断脚本的执行,并可能损坏数据,例如,非过渡数据库查询,建立配置文件等,而在您的情况下,它将下载部分文件...您可能会,或不,关心这一点。

相关问题