0

When I use this url http://www.ilpost.it/2014/02/25/peanuts-2014-febbraio-25/ (the page is empty but still exists in a certain sense) with file_get_contents it gives me the following warning:

Warning: file_get_contents(http://www.ilpost.it/2014/02/16/peanuts-2014-febbraio-16/) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found in [...my php url...]

Even if I put the function in a condition like this

if (file_get_contents($url_to_feed) === FALSE){..}

I get the warning and then the result of the condition.

How do I resolve this problem and avoid the warning?

Tom
  • 11
  • 7
  • 2
    Since that page is a 404, if it's your intended target, you could simply compress the warning by using `@file_get_contents();` http://www.php.net/manual/en/language.operators.errorcontrol.php – Sean3z Feb 16 '14 at 15:52
  • The URL gives a _404 Not Found_ status code, and that means it does not “exist” – that your browser shows you the HTML document that was delivered under that status code is a completely different matter. – CBroe Feb 16 '14 at 15:53
  • you could temporary set the level of error printing via: error_reporting(E_ERROR | E_PARSE); – serjoscha Feb 16 '14 at 15:53
  • 1
    @Sean3z post this as an answer, other than using cURL, this is about as good as it gets. – Michael Coxon Feb 16 '14 at 15:55

1 Answers1

-1

By using file_get_contents, you are making an assumption. You are assuming that the file exists.

On the local filesystem, you'd generally use file_exists to check for that first, however that would end up making two requests to the other server every time... so probably not a good idea.

Personally, I'd use sockets.

$fp = fsockopen("www.ilpost.it",80);
if( $fp) { // connection established
    fputs($fp,"GET /2014/02/25/peanuts-2015-febbraio-25/ HTTP/1.0\r\n"
             ."Host: www.ilpost.it\r\n"
             ."\r\n");
    $return = "";
    $headers = "firstline";
    while(!feof($fp)) {
        if( $headers) {
            $line = trim(fgets($fp));
            if( $headers == "firstline") {
                list(,$status,$text) = explode(" ",$line,3);
                if( $status != "200") { /* do something... or not! */ }
                $headers = "remaining";
            }
            if( !$line) $headers = false;
        }
        else $return .= fgets($fp);
    }
    fclose($fp);
}

Sure, it's a lot of code, but that's what defining your own functions is for ;)

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • Soo... anyone want to explain why this answer gets -2, or are you just gonna "downvote and run"? – Niet the Dark Absol Feb 16 '14 at 16:09
  • By default `file_get_contents()` only returns content of HTTP status 200. Using sockets provides no extra benefit here. As of PHP 5.0, you can specify a context for file_get_contents, allowing you to still receive the content for 404 (and other) pages. http://www.php.net/manual/en/context.http.php – Sean3z Feb 16 '14 at 16:13
  • @Sean3z Okay... but still, why two downvotes? With this code you have the basis to handle any status code in any way you want - notably, `500` for internal server errors, `403` if you're not allowed to access the resource... – Niet the Dark Absol Feb 16 '14 at 16:14