PHP streams and http 1.1 gotcha
I have a class that I wrote some time ago that is called URLImporter. It’s simple, does what it is supposed to do and thus makes my life easier. Essentially it helps me fetch URL resources – mostly dynamically generated data from password protected extranets that require me to use HTTP POST.
Up until recently this class have only been used behind an http proxy. The other day I moved it to another machine – that is not behind a proxy and suddenly it stopped working.
The code that was not working anymore was almost a school book example, somewhat simplified below.
while (!feof($this->fileHandle))
{
$response .= fread($this->fileHandle, 1024);
}
I started blaming this and that while searching for an answer as to why feof() stopped working, why it blocked indefinitely. It used to work. Obviously I looked in the wrong direction.
I used http 1.1 for communicating. A “new” feature in http 1.1 is that it by default keeps the connection alive for awhile. In http 1.0 you had to explicitly specify in the http headers that you wanted to use keep-alive.
So when I realized this I added the http header
Connection: close
and it did the trick. The reason it worked before I moved the script to the new machine was the http proxy that always closed the connection after each request.
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.
