我正在从 PHP 连续运行多个 cURL 请求。我运行请求的代码如下所示:
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json')
);
curl_exec($ch);
error_log("curl info: " . print_r(curl_getinfo($ch), true));
curl_close($ch);
第一个请求一切正常。来自的数据curl_getinfo
如下所示:
[url] => http://XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[content_type] => text/html; charset=UTF-8
[http_code] => 201
[header_size] => 492
[request_size] => 145
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.336019
[namelookup_time] => 0.140008
[connect_time] => 0.17601
[pretransfer_time] => 0.17601
[size_upload] => 2632
[size_download] => 178
[speed_download] => 529
[speed_upload] => 7832
[download_content_length] => 178
[upload_content_length] => 2632
[starttransfer_time] => 0.212012
[redirect_time] => 0
但是,第二个请求大约需要三秒钟。这反映在 中total_time
,但所有单独的时间值都很小:
[url] => http://XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[content_type] => text/html; charset=UTF-8
[http_code] => 201
[header_size] => 492
[request_size] => 145
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 3.572204
[namelookup_time] => 0
[connect_time] => 0.036002
[pretransfer_time] => 0.036002
[size_upload] => 2632
[size_download] => 178
[speed_download] => 49
[speed_upload] => 736
[download_content_length] => 178
[upload_content_length] => 2632
[starttransfer_time] => 0.068004
[redirect_time] => 0
total_time
即使连接和传输发生得很快,是什么导致如此长的?我还在服务器上记录了这个请求的时间(使用 error_log 和 PHP),直到我在客户端调用 curl_exec 后 3 秒才看到请求的开始。这是来自客户端的我的 PHP 和 cURL 版本信息:
$php -v
PHP 5.2.4-2ubuntu5.14 with Suhosin-Patch 0.9.6.2 (cli) (built: Jan 12 2011 15:54:52)
$curl -V
curl 7.18.0 (x86_64-pc-linux-gnu) libcurl/7.18.0 OpenSSL/0.9.8g zlib/1.2.3.3 libidn/1.1
Protocols: tftp ftp telnet dict ldap ldaps http file https ftps
Features: GSS-Negotiate IDN IPv6 Largefile NTLM SSL libz