HTTP streams are usable in 5.3.0

on Wed, 23 Jul 2008 at 23:35 in php solar with 1 comment

While working with my Solarified wrapper for Amazon's S3, I found out that prior to version 5.3.0, HTTP streams in PHP are almost unusable.

The problem comes when you make an HTTP request and the server responds with an error status code. What happens is that prior to version 5.3 the HTTP stream context considers an error status code a real PHP-level error. Now, instead of returning the response body, it returns false from functions like fread() and file_get_contents(). This is a huge problem with a service like S3 where you often get an error response with a body explaining what was wrong.

Fortunately, PHP 5.3.0 introduces a new HTTP stream context option ignore_errors which, once set on, will treat error statuses the same way as non-error statuses. I think this should definitely be the default behaviour.

I ended up using the excellent pecl_http extension for my HTTP requests. The result is a new HTTP request adapter for Solar: Lux_Http_Request_Adapter_Http (stupid name I know, will change that :-).

UPDATE: It's fread() and not fopen() that returns data from a stream. Fixed that.

Comments

avatar.php?gravatar_id=b4abe7d6fb932372ef98c2a9b393853e&rating=G&size=80&default=http%3A%2F%2Fanttih.com%2Fpublic%2FAnttih%2Fimages%2Fanon.png
Comment by Remco Tolsma Wed, 11 Feb 2009 (21:16)

If an webserver replies with an error status code the response content wil not be fetched (in PHP versions prior to 5.3.0). But you can access the response headers within the $http_response_header variable.

http://php.net/manual/en/reserved.variables.httpresponseheader.php

Add your comment

Will not be published. Finds your Gravatar.
Style guide