...

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



koyama
12-17-2008, 05: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:


<?php
// retrieve content from 404 error page
var_dump(file_get_contents('http://www.google.com/xyz'));

?>


Results:

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.
Output:


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.
Output:


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
bool(false)

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.

gsnedders
12-21-2008, 09: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.

koyama
12-21-2008, 10: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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum