Reputation: 9527
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] =>
Upvotes: 2
Views: 613
Reputation: 96424
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.
Upvotes: 2