本文介绍了cURL在PHP 7上非常慢,但在PHP 5上却不是的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与仅从命令行运行请求或在PHP5.6.24中运行时相比,使用PHP7.0.11对我来说 cURL似乎要慢得多。我正在使用以下代码对其进行测试:

  $ 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] =>
字符串(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)我的IP
[ local_port] =>
int(44555)
}

在运行PHP7时,我得到

  array(26){
[ url] =>
字符串(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] =>
浮动(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)我的IP
[ local_port] =>
int(55559)
}

重要的部分是total_time,即在PHP 5中为1.3秒,在PHP 7中为16.9s。



在请求上设置超时后,接收到的字节数与超时成正比-数据为传输非常缓慢,而不是因为有障碍物阻止了一段时间的传输,然后整个传输就一口气传输了。



运行Debian,我似乎无法在Fedora本地计算机上重现该问题。

解决方案

与PHP 7相同的问题Debian拉伸。我注意到较高的sys-cpu时间:0.07s用户10.02s系统92%cpu 10.859总计



在禁用transparent_hugepage后问题已解决:

 回声永不> / sys / kernel / mm / transparent_hugepage / enabled 
echo never> / sys / kernel / mm / transparent_hugepage / defrag

它不会直接影响curl下载,但会降低速度PHP的内存分配。请参阅:


cURL seems to be significantly slower for me using PHP7.0.11 than it is when just running the request from the command line, or when run within PHP5.6.24. I'm testing it using the following code:

$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));

In both PHP5 and PHP7's CLI interpreter, and in PHP5 I get

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)
}

While when running PHP7, I get

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)
}

The important part being total_time, which is 1.3 seconds in PHP 5 but 16.9s in PHP 7.

When a timeout is set on the request, the number of bytes received is proportional to the timeout - the data is being transferred very slowly, rather than there being some obstruction that is preventing anything from being transferred for a while, then the whole thing being transferred in one go.

The server is running Debian, and I can't seem to reproduce the issue on my Fedora local machine.

解决方案

Same issue with PHP 7 on Debian Stretch. I noticed high sys-cpu times: 0.07s user 10.02s system 92% cpu 10.859 total

Issue was solved after disable transparent_hugepage:

echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag

It does not effect curl downloads directly, but slows down php memory allocations. See: https://serverfault.com/questions/780555/how-to-troubleshoot-high-load-caused-by-php7

这篇关于cURL在PHP 7上非常慢,但在PHP 5上却不是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 19:33