对于我来说,使用 PHP7.0.11 时,cURL 似乎比仅从命令行运行请求或在 PHP5.6.24 中运行时慢得多。我正在使用以下代码对其进行测试:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://i.imgur.com/H1zC601.gif");
curl_setopt($curl, CURLOPT_HTTPGET, TRUE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($curl);
var_dump(curl_getinfo($curl));
在 PHP5 和 PHP7 的 CLI 解释器中,在 PHP5 中我得到
array(26) {
["url"]=>
string(31) "https://i.imgur.com/H1zC601.gif"
["content_type"]=>
string(9) "image/gif"
["http_code"]=>
int(200)
["header_size"]=>
int(597)
["request_size"]=>
int(204)
["filetime"]=>
int(-1)
["ssl_verify_result"]=>
int(0)
["redirect_count"]=>
int(0)
["total_time"]=>
float(1.260002)
["namelookup_time"]=>
float(0.060424)
["connect_time"]=>
float(0.068474)
["pretransfer_time"]=>
float(0.089705)
["size_upload"]=>
float(0)
["size_download"]=>
float(34327108)
["speed_download"]=>
float(27243693)
["speed_upload"]=>
float(0)
["download_content_length"]=>
float(34327108)
["upload_content_length"]=>
float(-1)
["starttransfer_time"]=>
float(0.098354)
["redirect_time"]=>
float(0)
["redirect_url"]=>
string(0) ""
["primary_ip"]=>
string(15) "151.101.124.193"
["certinfo"]=>
array(0) {
}
["primary_port"]=>
int(443)
["local_ip"]=>
string(14) "my IP"
["local_port"]=>
int(44555)
}
当运行 PHP7 时,我得到
array(26) {
["url"]=>
string(31) "https://i.imgur.com/H1zC601.gif"
["content_type"]=>
string(9) "image/gif"
["http_code"]=>
int(200)
["header_size"]=>
int(609)
["request_size"]=>
int(61)
["filetime"]=>
int(-1)
["ssl_verify_result"]=>
int(0)
["redirect_count"]=>
int(0)
["total_time"]=>
float(16.875167)
["namelookup_time"]=>
float(0.252648)
["connect_time"]=>
float(0.260626)
["pretransfer_time"]=>
float(0.280489)
["size_upload"]=>
float(0)
["size_download"]=>
float(34327108)
["speed_download"]=>
float(2034178)
["speed_upload"]=>
float(0)
["download_content_length"]=>
float(34327108)
["upload_content_length"]=>
float(-1)
["starttransfer_time"]=>
float(0.288715)
["redirect_time"]=>
float(0)
["redirect_url"]=>
string(0) ""
["primary_ip"]=>
string(15) "151.101.124.193"
["certinfo"]=>
array(0) {
}
["primary_port"]=>
int(443)
["local_ip"]=>
string(14) "my IP"
["local_port"]=>
int(55559)
}
重要的部分是total_time,在 PHP 5 中为 1.3 秒,但在 PHP 7 中为 16.9 秒。
当请求设置超时时,接收到的字节数与超时成正比 - 数据传输非常缓慢,而不是存在一些阻碍暂时阻止任何内容传输,然后整个事情就发生了一口气转移。
服务器运行的是 Debian,我似乎无法在 Fedora 本地计算机上重现该问题。