Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Senior Coder koyama's Avatar
    Join Date
    Dec 2006
    Location
    Copenhagen, Denmark
    Posts
    1,246
    Thanks
    1
    Thanked 5 Times in 5 Posts

    file_get_contents(<url>) not working with 404 error pages?

    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 Code:
    <?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:

    Code:
    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:

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

  • #2
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts
    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.

  • #3
    Senior Coder koyama's Avatar
    Join Date
    Dec 2006
    Location
    Copenhagen, Denmark
    Posts
    1,246
    Thanks
    1
    Thanked 5 Times in 5 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •