KDV
KDV

Reputation: 730

PHP Curl Slowness

For some reason my curl call is very slow. Here is the code I used.

$postData = "test"
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$result = curl_exec($ch);

Executing this code takes on average 250ms to finish. However when I just open the url in a browser, firebug says it only takes about 80ms.

Is there something I am doing wrong? Or is this the overhead associated with PHP Curl.

It's the call to

curl_exec

That is taking up all the time.

UPDATE:

So I figured out right after I posted this that if I set the curl option

curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

It significantly slows down

curl_exec

The post data could be anything and it will slow it down.

Even if I set

curl_setopt($ch, CURLOPT_POST, false);

It's slow.

I'll try to work around it by just adding the parameters to the URI as a query string.

SECOND UPDATE:

Confirmed that if I just call the URI using GET and passing parameters as a query string it is much faster than using POST and putting the parameters in the body.

Upvotes: 23

Views: 42011

Answers (8)

Anse
Anse

Reputation: 1660

I just experienced a massive speed-up through compression. By adding the Accept-Encoding header to "gzip, deflate", or just to all formats which Curl supports, my ~200MB download took 6s instead of 20s:

curl_setopt($ch, CURLOPT_ENCODING, '');

Notes:

  • If an empty string, "", is set, a header containing all supported encoding types is sent.
  • you do not even have to care about decompression after the download, as this is done by Curl internally.
  • CURLOPT_ENCODING requires Curl 7.10+

Upvotes: 0

Stephen R
Stephen R

Reputation: 3927

I just resolved this exact problem by removing the following two options:

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

Somehow on the site I was fetching, the POST request to over ten full seconds. If it's GET, it's less than a second.

So... in my wrapper function that does the Curl requests, it now only sets those two options when there is something in $postData

Upvotes: 0

Diógenes Vidotto
Diógenes Vidotto

Reputation: 11

Adding "curl_setopt($ch, CURLOPT_POSTREDIR, CURL_REDIR_POST_ALL);" solved here. Any problem with this solution?

Upvotes: 1

eagle
eagle

Reputation: 137

try this

curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );

Upvotes: 12

Brent Baisley
Brent Baisley

Reputation: 12721

Curl has the ability to tell exactly how long each piece took and where the slowness is (name lookup, connect, transfer time). Use curl_getinfo (http://www.php.net/manual/en/function.curl-getinfo.php) after you run curl_exec.

If curl is slow, it is generally not the PHP code, it's almost always network related.

Upvotes: 21

Roman Newaza
Roman Newaza

Reputation: 11700

CURL has some problems with DNS look-ups. Try using IP address instead of domain name.

Upvotes: 43

s1lence
s1lence

Reputation: 2188

The curl functions in php directly use the curl command line tool under *nix systems.

Therefore it really only depends on the network speed since in general curl itself is much faster than a webbrowser since it (by default) does not load any additional data like included pictures, stylesheets etc. of a website.

It might be possible that you are not aware, that the network performance of the server on which you were testing your php script is way worse than on your local computer where you were testing with the browser. Therefore both measurements are not really comparable.

Upvotes: -1

Jaspreet Chahal
Jaspreet Chahal

Reputation: 2750

generally thats acceptable when you are loading contents or posting to slower end of world. curl call are directly proportional to your network speed and throughput of your webserver

Upvotes: -2

Related Questions