user3110491
user3110491

Reputation: 11

Very slow curl compared to browser

What may be the reason for this simple code:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://somepage.com/path');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERIFYPEER, false);
$output = curl_exec($ch);

be consistently 1+ seconds slower than Firefox is?

I have tested the PHP code using

$timestart = microtime(true);
echo microtime(true) - $timestart;

and Firebug (Net tab) on Firefox.

https://somepage.com/path returns plain JSON, it takes around 500ms on Firefox and 1500 with cURL in PHP.

Upvotes: 1

Views: 916

Answers (2)

Sabuj Hassan
Sabuj Hassan

Reputation: 39405

Accept-Encoding gzip, deflate

Firefox always asks for compressed content to the remote site(if available). I guess in your case, the site is returned the compressed html through the browser. But while fetching the html using curl, you didn't set anything like that in your code.

Try to add this on your curl code and check the performance again.

curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate'); 

If no change found, then try below one.

curl_setopt($ch, CURLOPT_ENCODING, ""); // supports all

Upvotes: 1

PHPer
PHPer

Reputation: 1

You can try sending Firefox's user agents in cURL.

Upvotes: 0

Related Questions