2

In my script, if I use directly file_get_content and download file, result is within 1 second.

However, if I use CURL, I wait around 5 seconds. This is my code, is there something wrong?

$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 64);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_URL, $url);

$dataStream = curl_exec($ch);
if ($dataStream === false)
{
    $lastError = curl_error($ch);
}
curl_close($ch);

//process $dataStream

Both connections are with same $url that is on HTTPS protocol.

Output from curl_getinfo on my primary server:

[content_type] => text/html [http_code] => 200 [header_size] => 433 [request_size] => 214 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 5.336287 [namelookup_time] => 0.000472 [connect_time] => 0.000722 [pretransfer_time] => 0.121571 [size_upload] => 0 [size_download] => 398 [speed_download] => 74 [speed_upload] => 0 [download_content_length] => 398 [upload_content_length] => 0 [starttransfer_time] => 0.33125 [redirect_time] => 0 [redirect_url] =>

I also tried the same code on my localhost machine and it works just fine with CURL. Output from curl_getinfo on my localhost test server:

[content_type] => text/html [http_code] => 200 [header_size] => 433 [request_size] => 207 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.328 [namelookup_time] => 0 [connect_time] => 0.031 [pretransfer_time] => 0.109 [size_upload] => 0 [size_download] => 376 [speed_download] => 1146 [speed_upload] => 0 [download_content_length] => 376 [upload_content_length] => -1 [starttransfer_time] => 0.328 [redirect_time] => 0 [redirect_url] =>

7
  • 1
    The results of curl_getinfo() might give you some insight. Commented Dec 28, 2017 at 16:47
  • 2
    Is that 64 for the buffer size limiting the transfer to chunks of 64 bytes at a time? That could explain it being slow. Try 1024 * 64 or something larger. Commented Dec 28, 2017 at 16:51
  • @RussellAustin Edited question Commented Dec 28, 2017 at 16:55
  • 1
    What's the buffer size setting supposed to achieve here anyway? Is there a specific requirement to explicitly set/use one? (By the API/service you are requesting data from?) I don't suppose file_get_contents bothers with such specifics much ... Commented Dec 28, 2017 at 16:57
  • @CBroe It seems that removing this solved the problem. Thank you. Post it as answer please. Commented Dec 28, 2017 at 17:01

1 Answer 1

2

What's the buffer size setting supposed to achieve here?

According to the manual,

The size of the buffer to use for each read. There is no guarantee this request will be fulfilled, however.

it seems to be the factor slowing things down here. By limiting the read buffer size, reading slows down with it - kinda understandable ;-) It might have a much higher default value, or no buffering happening at all unless specified - so unless there is a specific requirement to do so by the API/endpoint you are making the request to, likely best not to specify it at all.

Sign up to request clarification or add additional context in comments.

1 Comment

fwiw the default CURLOPT_BUFFERSIZE is 16384... so setting it down to 64 should multiply the number of fwrite() (well, CURLOPT_WRITEFUNCTION calls, which default to fwrite) by roughly 1024! so if the original bufsize caused ... idk, 10 fwrite calls, this will call fwrite 10240 times instead of 10!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.