Interesting Bug in the HTTP Streams Wrapper

Streams are quite possibly one of the coolest things about PHP. They’re a feature of the core and allow you to do some basic things that might otherwise require a separate extension, which may or may not be available if you’re in a shared hosting environment. Among these things is acting as an HTTP client, which you can do using the HTTP streams wrapper. See Example #1 on that page for a code sample showing how to submit a POST request.

I wrote a small script a while back that’s gained a surprising amount of popularity thanks to a plug from the site that it posts to. The current incarnation of the script uses the cURL extension to send a POST request to, the response from which it then parses for the URL corresponding to the code that was originally sent. When I learned that this could be done with streams, I attempted to implement it in that fashion, but ran into strange issues where I would get 404 or 500-level HTTP errors rather than the response I was expecting.

After some digging, it turns out that this is a bug in the 5.2.x branch. The issue has to do with how headers are arranged by the underlying C code. As a result, explicitly specifying a Content-Type header for the operation will result in failure. However, not explicitly specifying the Content-Type header value results in a Notice being output and the correct header value being used automatically, which coincidentally causes the operation to succeed.

The bug has been fixed in the 5.3 and 6 branches and is expected to be fixed in 5.2.6 as well. Hope this workaround proves helpful to anyone who runs into a similar issue.

One Comment

  1. … posts in Development” rel=”category tag”>Development Matthew Turland has come across an “interesting bug” in PHP’s stream wrappers functionality – some strange 404 or 500 HTTP errors in one of his sc…