Reputation: 245
I have a web request from 1 server to another that is taking 1 second. The same request in my browser takes almost no time. I have looked through the various similar questions and tried all the offered suggestions I could find including
None of these has made any impact on the time. Here are the ways I tried this:
$timeBefore = microtime(true);
//Only one of these active at a time
$context = stream_context_create(array('http' => array('header'=>'Connection: close\r\n')));
$output = file_get_contents($url,false,$context);
//Or
$context=stream_context_create(array('http' => array('header'=>"Host: www.google.com\r\n")));
$output = file_get_contents($url, false, $context);
//Or
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
$timeAfter = microtime(true);
and then log to a file $timeAfter - $timeBefore
. I always get around 1.04202604294 as the result and the client app that uses this actually sees the 1 second delay.
As a side note in case it is relevant, the 2 servers here are talking in the following manner A to B, during B's answer to A it sends this URL back to A, then takes all that and sends its original response back to A. So there is a connection back to the source during the connection from the source.
Here is the debug output from curl_getinfo
content_type => text/html,
http_code => 200,
header_size => 219,
request_size => 479,
filetime => -1,
ssl_verify_result => 0,
redirect_count => 0,
total_time => 1.04360199999999991860022419132292270660400390625,
namelookup_time => 0.0001579999999999999915796522476085783637245185673236846923828125,
connect_time => 0.0007219999999999999855393451042573360609821975231170654296875,
pretransfer_time => 0.000772000000000000008264222639553508997778408229351043701171875,
size_upload => 0,
size_download => 1,
speed_download => 0,
speed_upload => 0,
download_content_length => -1,
upload_content_length => -1,
starttransfer_time => 1.0435689999999999688640173189924098551273345947265625,
redirect_time => 0,
redirect_url =>
This command is being sent from 2 servers on the same hosting service to each other, and they are both not under load and its a high quality hosting site(liquid web dedicated server). The response and url are both very small
Upvotes: 0
Views: 51
Reputation: 23958
My experience is that it takes time to contact another page/server.
What you can do is to limit the number of connections it does by caching the data for x minutes. (if possible)
I cache my data to a txt file with put_file_contents() and then use filemtime() to see how old the file is.
This way you only need to read from your local txt file with file_get_contents() to get the data.
This will speed it up, but it also means you may display "dated" data.
Upvotes: 0
Reputation: 3491
This is something almost impossible to diagnose without running the exact same code and connecting to the same site in our browsers. But in general, your browser is going to do more to negotiate a fast connection with the server than CURL. This includes supporting HTTP/2 & gzip, both of which make data transfer faster from end to end.
Upvotes: 2