gumape
gumape

Reputation: 2932

Curl slow sometimes

The same curl_exec is called every two seconds on my wamp server. In most cases the total_time is about 0.3 seconds, but in some cases (roughly every 30th call, but not deterministic) it is 8-9 secs.

curl_getinfo (fast):

[content_type] => application/json
[http_code] => 200
[header_size] => 141
[request_size] => 878
[filetime] => -1
[ssl_verify_result] => 20
[redirect_count] => 0
[total_time] => 0.312
[namelookup_time] => 0
[connect_time] => 0.047
[pretransfer_time] => 0.156
[size_upload] => 635
[size_download] => 45528
[speed_download] => 145923
[speed_upload] => 2035
[download_content_length] => -1
[upload_content_length] => 635
[starttransfer_time] => 0.203
[redirect_time] => 0
[redirect_url] => 

curl_getinfo (slow):

[content_type] => application/json
[http_code] => 200
[header_size] => 141
[request_size] => 878
[filetime] => -1
[ssl_verify_result] => 20
[redirect_count] => 0
[total_time] => 8.469
[namelookup_time] => 0
[connect_time] => 0.047
[pretransfer_time] => 0.703
[size_upload] => 635
[size_download] => 51340
[speed_download] => 6062
[speed_upload] => 74
[download_content_length] => -1
[upload_content_length] => 635
[starttransfer_time] => 2.531
[redirect_time] => 0
[redirect_url] => 

Every case when total_time is high, starttransfer_time is also high. Additionally, pretransfer_time is higher while speed_download is lower than usual.

I've tried:

curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );

and to use IP instead of the domain name in the URL, but did not solve the problem.

Has anyone any ideas on what could be causing this?

Upvotes: 2

Views: 1930

Answers (1)

ygor
ygor

Reputation: 1756

I had a problem with slow cURL too. I was implementing a "proxy" in PHP using cURL. I made a stupid mistake though:

  1. I forwarded all headers except "Host"
  2. I modified the payload

In my case, the problem was in the "Content-Length" header, which I forwarded too.

I made the payload shorter by replacing some values in the string. I assume, that the cURL library was expecting more bytes to be sent. This lead to a 20 - 30 seconds timeout.

The fix was not to forward the "Content-Length" header too. Hope this helps to some of you.

Upvotes: 1

Related Questions