Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Curl Slowness

Tags:

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.

like image 408
KDV Avatar asked Feb 16 '12 02:02

KDV


2 Answers

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

like image 72
Roman Newaza Avatar answered Oct 06 '22 08:10

Roman Newaza


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.

like image 22
Brent Baisley Avatar answered Oct 06 '22 07:10

Brent Baisley