Edit report at https://bugs.php.net/bug.php?id=61336&edit=1
ID: 61336
Updated by: ramsey@php.net
Reported by: ramsey@php.net
Summary: file_get_contents() no longer returns false on 4xx
responses
Status: Open
Type: Bug
Package: Filesystem function related
Operating System: CentOS 6.2
PHP Version: 5.4.0
Block user comment: N
Private report: N
New Comment:
On that same Debian 6.0.4 VM (using VirtualBox), I built PHP from the latest checkout of the PHP-5.4
branch. php --version gives me the following version line: PHP 5.4.1RC1-dev (cli)
(built: Mar 29 2012 18:34:37)
When I run my test script with this build of PHP, I am still having the same problem.
Previous Comments:
------------------------------------------------------------------------
[2012-03-13 20:24:57] ramsey@php.net
I've just tried this on a clean Debian 6.0.4 virtual machine, and I'm having the
same problem there (I've tried even with ignore_errors set to false).
Here are the PHP build notes for my Debian installation: http://pastie.org/3588244
------------------------------------------------------------------------
[2012-03-13 15:20:20] ramsey@php.net
I'm still seeing the problem with ignore_errors set to false. See below for how
I'm setting ignore_errors. For full details on how my environment is set up, you
can refer to http://benramsey.com/blog/2012/03/build-php-54-on-centos-62/.
<?php
$context = stream_context_create(array(
'http' => array(
'ignore_errors' => false
)
));
$response =
file_get_contents('http://us3.php.net/manual/en/function.foobar.php',
false,
$context);
var_dump($http_response_header);
var_dump($response);
------------------------------------------------------------------------
[2012-03-10 14:21:41] cataphract@php.net
I can't reproduce this. Probably the default context has ignore_errors on. Verify that it
doesn't and try to pass a context that has ignore_errors set to false.
------------------------------------------------------------------------
[2012-03-09 22:41:50] seld@php.net
Just for the record, repro script works for me on Windows / 5.4.0 VC9 NTS
------------------------------------------------------------------------
[2012-03-09 21:59:47] ramsey@php.net
Description:
------------
In PHP 5.3, file_get_contents() returns false on 4xx responses. In PHP 5.4,
file_get_contents() is returning the actual response body, rather than false.
Test script:
---------------
<?php
$response = file_get_contents('http://us3.php.net/manual/en/function.foobar.php');
var_dump($http_response_header);
var_dump($response);
Expected result:
----------------
With warnings turned on, this is what I get in PHP 5.3 and what I expect to see
in PHP 5.4:
PHP Warning:
file_get_contents(http://us3.php.net/manual/en/function.foobar.php): failed to
open stream: HTTP request failed! HTTP/1.0 404 Not Found
in /Users/ramsey/Desktop/file_get_contents.php on line 3
PHP Stack trace:
PHP 1. {main}() /Users/ramsey/Desktop/file_get_contents.php:0
PHP 2. file_get_contents() /Users/ramsey/Desktop/file_get_contents.php:3
array(11) {
[0]=>
string(22) "HTTP/1.0 404 Not Found"
[1]=>
string(35) "Date: Fri, 09 Mar 2012 21:57:32 GMT"
[2]=>
string(29) "Server: Apache/2.2.3 (CentOS)"
[3]=>
string(23) "X-Powered-By: PHP/5.3.2"
[4]=>
string(20) "Content-language: en"
[5]=>
string(88) "Set-Cookie: LAST_LANG=en; expires=Sat, 09-Mar-2013 21:57:32 GMT;
path=/; domain=.php.net"
[6]=>
string(102) "Set-Cookie: COUNTRY=USA%2C64.2.187.194; expires=Fri, 16-Mar-2012
21:57:32 GMT; path=/; domain=.php.net"
[7]=>
string(21) "Status: 404 Not Found"
[8]=>
string(20) "Content-Length: 4219"
[9]=>
string(17) "Connection: close"
[10]=>
string(37) "Content-Type: text/html;charset=utf-8"
}
bool(false)
Actual result:
--------------
array(11) {
[0]=>
string(22) "HTTP/1.1 404 Not Found"
[1]=>
string(35) "Date: Fri, 09 Mar 2012 21:58:44 GMT"
[2]=>
string(29) "Server: Apache/2.2.3 (CentOS)"
[3]=>
string(23) "X-Powered-By: PHP/5.3.2"
[4]=>
string(20) "Content-language: en"
[5]=>
string(88) "Set-Cookie: LAST_LANG=en; expires=Sat, 09-Mar-2013 21:58:44 GMT;
path=/; domain=.php.net"
[6]=>
string(102) "Set-Cookie: COUNTRY=USA%2C64.2.187.194; expires=Fri, 16-Mar-2012
21:58:44 GMT; path=/; domain=.php.net"
[7]=>
string(21) "Status: 404 Not Found"
[8]=>
string(20) "Content-Length: 4219"
[9]=>
string(17) "Connection: close"
[10]=>
string(37) "Content-Type: text/html;charset=utf-8"
}
string(4219) "<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xml:lang="en" lang="en">
<head>
<title>PHP: 404 Not Found</title>
<style type="text/css" media="all">
@import url("/styles/site.css");
@import url("/styles/mirror.css");
...
The rest of the HTML output from the php.net 404 page.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=61336&edit=1