PHP file_get_contents very slow when using full url

Note: This has been fixed in PHP 5.6.14. A Connection: close header will now automatically be sent even for HTTP/1.0 requests. See commit 4b1dff6.

I had a hard time figuring out the cause of the slowness of file_get_contents scripts.

By analyzing it with Wireshark, the issue (in my case and probably yours too) was that the remote web server DIDN’T CLOSE THE TCP CONNECTION UNTIL 15 SECONDS (i.e. “keep-alive”).

Indeed, file_get_contents doesn’t send a “connection” HTTP header, so the remote web server considers by default that’s it’s a keep-alive connection and doesn’t close the TCP stream until 15 seconds (It might not be a standard value – depends on the server conf).

A normal browser would consider the page is fully loaded if the HTTP payload length reaches the length specified in the response Content-Length HTTP header. File_get_contents doesn’t do this and that’s a shame.

SOLUTION

SO, if you want to know the solution, here it is:

$context = stream_context_create(array('http' => array('header'=>'Connection: close\r\n')));
file_get_contents("http://www.something.com/somepage.html",false,$context);

The thing is just to tell the remote web server to close the connection when the download is complete, as file_get_contents isn’t intelligent enough to do it by itself using the response Content-Length HTTP header.

Leave a Comment