Bug #67802 [NEW]: Streams API cannot return error body/content
| From: | chad at getfidelis dot com | Date: | Thu, 07 Aug 2014 14:51:44 +0000 |
| Subject: | Bug #67802 [NEW]: Streams API cannot return error body/content | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-187010@lists.php.net to get a copy of this message | ||
From: chad at getfidelis dot com
Operating system: Linux
PHP version: 5.5.15
Package: Streams related
Bug Type: Bug
Bug description:Streams API cannot return error body/content
Description:
------------
I have been unable to find any way to access body content using the HTTP
stream wrapper, if the response contained an HTTP error code.
I'm trying to contact a REST API that returns useful error data in the
body (pinpointing, for instance, _which parameter_ is incorrect and what
the proper range or format for its value is) with error codes 400 and
up. When this error code is returned, fopen() returns FALSE, and it's
not possible to access the response.
$http_response_header contains the *headers* but not the body. All of
the stream functions require a resource, which fopen() clearly didn't
return, and substituting a context passed to the fopen() call also fails
(which I expected, but tried anyway.) There does not seem to be any
sort of stream_get_last_error() function, either.
I can pull this off with cURL of course, but the streams API is
generally more convenient to use.
Test script:
---------------
<?php
# I can't give you my password for the API, so I'll use google...
$fp = fopen("https://www.google.com/please-404-forever?thanks=1",
'r');
print_r($http_response_header); # does not have body
print_r(array_keys($GLOBALS)); # no useful vars present
if ($fp)
echo 'got a stream', PHP_EOL; # never runs
?>
--
Edit bug report at https://bugs.php.net/bug.php?id=67802&edit=1
--