Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p><strong>Note:</strong> This has been fixed in PHP 5.6.14. A <code>Connection: close</code> header will now automatically be sent even for HTTP/1.0 requests. <a href="https://github.com/php/php-src/commit/4b1dff6f438f84f7694df701b68744edbdd86153" rel="noreferrer">See commit <code>4b1dff6</code></a>.</p> </blockquote> <p>I had a hard time figuring out the cause of the slowness of file_get_contents scripts.</p> <p>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").</p> <p>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).</p> <p>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.</p> <p><strong>SOLUTION</strong></p> <p>SO, if you want to know the solution, here it is:</p> <pre><code>$context = stream_context_create(array('http' =&gt; array('header'=&gt;'Connection: close\r\n'))); file_get_contents("http://www.something.com/somepage.html",false,$context); </code></pre> <p>The thing is just to <strong>tell the remote web server to close the connection when the download is complete</strong>, as file_get_contents isn't intelligent enough to do it by itself using the response Content-Length HTTP header.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload