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本地机器上重现这个问题。
在Debian Stretch上的PHP 7也有同样的问题。我注意到很高的系统-cpu时间:0.07秒用户10.02秒系统92% cpu 10.859总
禁用transparent_hugepage:
后问题得到解决echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag
它不会直接影响curl下载,但会减慢php内存分配。参见:https://serverfault.com/questions/780555/how-to-troubleshoot-high-load-caused-by-php7
对我来说,这只是一个网络延迟问题或带宽问题。如果您检查两者之间的speed_download
,您将看到其中一个下载速度明显快于另一个。使用PHP 5/7-CLI的第一个结果显示"speed_download" => float(27243693)
,而第二个结果显示"speed_download" => float(2034178)
,而它们都具有相同的内容长度"download_content_length" => float(34327108)
。
所以第一个以~25.9MB/秒的速度下载一个~32.7MB的文件,而第二个以~1.9MB/秒的速度下载相同的文件。
我有同样的问题与curl onPHP 7.0.12我运行在cli和fpm模式和下载非常慢的速度,速度从100Mbps下降到只有1~2 Mbps/s,我切换到php 5.6和问题解决,所以它不是网络问题,我确信希望我的信息能帮到别人
php-curl的版本如下:
php5 --ri curl
php7 --ri curl