php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #67802 Streams API cannot return error body/content
Submitted: 2014-08-07 14:51 UTC Modified: 2014-08-07 15:12 UTC
From: chad at getfidelis dot com Assigned:
Status: Not a bug Package: Streams related
PHP Version: 5.5.15 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: chad at getfidelis dot com
New email:
PHP Version: OS:

 

 [2014-08-07 14:51 UTC] chad at getfidelis dot com
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
?>


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-08-07 15:12 UTC] aharvey@php.net
-Status: Open +Status: Not a bug
 [2014-08-07 15:12 UTC] aharvey@php.net
You can set the ignore_errors context option to always retrieve the body, per http://ca2.php.net/manual/en/context.http.php.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sat Oct 19 08:01:28 2019 UTC