|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2012-03-09 21:59 UTC] 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.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 19:00:01 2025 UTC |
I actually get the same issue on both 5.3.11 and 5.4.4: 5.3.11: ------- array(11) { [0]=> string(22) "HTTP/1.1 404 Not Found" [1]=> string(35) "Date: Thu, 18 Oct 2012 15:35:04 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=Fri, 18-Oct-2013 15:35:04 GMT; path=/; domain=.php.net" [6]=> string(101) "Set-Cookie: COUNTRY=GBR%2C94.195.8.27; expires=Thu, 25-Oct-2012 15:35:04 GMT; path=/; domain=.php.net" [7]=> string(21) "Status: 404 Not Found" [8]=> string(20) "Content-Length: 4182" [9]=> string(17) "Connection: close" [10]=> string(37) "Content-Type: text/html;charset=utf-8" } string(4182) "<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 5.4.4: ------- array(11) { [0] => string(22) "HTTP/1.1 404 Not Found" [1] => string(35) "Date: Thu, 18 Oct 2012 15:35:19 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=Fri, 18-Oct-2013 15:35:19 GMT; path=/; domain=.php.net" [6] => string(101) "Set-Cookie: COUNTRY=GBR%2C94.195.8.27; expires=Thu, 25-Oct-2012 15:35:19 GMT; path=/; domain=.php.net" [7] => string(21) "Status: 404 Not Found" [8] => string(20) "Content-Length: 4182" [9] => string(17) "Connection: close" [10] => string(37) "Content-Type: text/html;charset=utf-8" } string(4182) "<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"\nI think I have new info on this issue. If you check the URI at the bottom you'll find three tests: 1. Using file_file_get_contents() on an non-existing resource on a existing domain: Result : empty string $http_response_header : an array 2. Using file_file_get_contents() on a non-existing domain, with follow_location => 0 on the context: Result : empty string $http_response_header : NULL 3. Using file_file_get_contents() on a non-existing domain, __without__ follow_location => 0 on the context: Result : non empty string $http_response_header : an array I'd expect that all the results should be NULL. I think the problem has to to with idiosyncratic behavior on some internal library. In the third test we can see that there's something asking OpenDNS when trying to deal with the non-existing behavior; and then it sends a Location header. This behavior is not documented. - The same script on a Windows 7/Apache Server [PHP 5.4.12] works as expected; - On a Debian 3.2.35-2 running PHP 5.2.5 it works as expected; - On a Turnkey Linux (Ubuntu based Linux) running PHP 5.3.3-7+squeeze14 it works as expected; - On a Ubuntu running PHP 5.3.3-7+squeeze14 it works as expected. So I'd say it has to do with the underlying system libraries and configuration; it would be nice if others test the script on other kind of servers... Test script: http://maisqi/outros/bugs/php/61336 [CentOS / PHP 5.4.11]I've run the test case that maisqi provided, and here are my results: 1. Using file_file_get_contents() on an non-existing resource on a existing domain: The result: boolean $http_response_header: array 2. Using file_file_get_contents() on a non-existing domain, with follow_location => 0 on the context: The result: boolean $http_response_header: NULL 3. Using file_file_get_contents() on a non-existing domain, __without__ follow_location => 0 on the context: The result: boolean $http_response_header: NULL This is on Ubuntu 12.04 LTS 64-bit with Apache 2.2.22 and PHP 5.3.10-1ubuntu3.5 with the Suhosin-Patch. Everything appears to be working fine there. Shouldn't the output of the results be a boolean false, not NULL, maisqi?