View Full Version : file_get_contents(<url>) not working with 404 error pages?

12-17-2008, 06:19 AM
I am having trouble porting a PHP script. It all comes down to some strange behavior of file_get_contents(<url>) together with 404 pages.

Test script:

// retrieve content from 404 error page



1. My local server (PHP Version 5.2.6-pl7-gentoo):

Does not seem to bother that it is a 404 error page. It reads the URL as if it was a completely normal page.

string(7200) "<html><head><meta http-equiv=content-type content="text/html;
charset=ISO-8859-1"><title>404 - Page Not Found</title>..."

2. Web server (PHP Version 5.2.8-0.dotdeb.1):

Refuses to retrieve contents of error page.

Warning: file_get_contents(http://www.google.com/xyz) [function.file-get-contents]:
failed to open stream: HTTP request failed! HTTP/1.0
404 Not Found in /home/www/ab/index.php on line 3

What is the reason for the difference?

I have done plenty of searches, but I haven't been able to find anyone having ”success” like me retrieving content from error pages using file_get_contents(). So which server is working correctly? Where does it even say in the PHP documentation what should happen with file_get_contents() in the case of 404 error pages?

I am aware that there is the more powerful cURL library for doing similar things, but at this point I would just like to know the reason for the above difference.

12-21-2008, 10:02 AM
PHP 5.2 deliberately changed behaviour — I can't find a link quickly, but PHP 5.2.8's behaviour is what is meant. Only 200 responses (and anything else that maps to that, like any unknown 2xx responses) are returned. Redirections are gracefully handled.

12-21-2008, 11:08 AM
Thank you very much for your response. I had almost given up on this one thinking that it was related to the PHP configuration. It makes perfect sense if this is due to changed behavior of PHP. Thanks, once again.