6

I succesfully fetch websites with

file_get_contents("http://www.site.com");

However, if the url doesn't exist, or not reachable, I am getting

Warning: file_get_contents(http://www.site.com) [function.file-get-contents]: 
failed to open stream: operation failed in /home/track/public_html/site.php 
on line 773

Is it possible to echo "Site not reachable"; instead of the error ?

user198989
  • 4,574
  • 19
  • 66
  • 95

5 Answers5

8

You can use the silence operator @ together with $php_errormsg:

if(@file_get_contents($url) === FALSE) {
    die($php_errormsg);
}

Where the @ suppresses the error message, the message text will be available for output in $php_errormsg

But note that $php_errormsg is disabled by default. You'll have to turn on track_errors. So add at the top of your code:

ini_set('track_errors', 1);

However there is a way that does not depend on track errors:

if(@file_get_contents($url) === FALSE) {
    $error = error_get_last();
    if(!$error) {
        die('An unknown error has occured');
    } else {
        die($error['message']);
    }
}
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
6

I would perfer to trigger exceptions instead of error messages:

function exception_error_handler($errno, $errstr, $errfile, $errline ) {
    // see http://php.net/manual/en/class.errorexception.php
    throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
}

set_error_handler("exception_error_handler");

Now you can catch the error like this:

try {
    $content = file_get_contents($url);
} catch (ErrorException $ex) {
    echo 'Site not reachable (' . $ex->getMessage() . ')';
}
sroes
  • 14,663
  • 1
  • 53
  • 72
  • 1
    But you will get yourself into deep trouble messing around with the error handler like this – crackmigg May 25 '13 at 13:37
  • `Working with @ can indeed be considered as wrong` LOL :D Yes, you presented a nice alternative but be careful when saying such things – hek2mgl May 25 '13 at 13:40
  • @migg You could always restore the error handler with `restore_error_handler` – sroes May 25 '13 at 13:41
  • But you did not and did not mention it. And you do not `die()` in your error handler which lets all error code continue after throwing your exception... which can also be considered as wrong. – crackmigg May 25 '13 at 13:43
  • @hek2mgl If you really don't care about what error you got, I get why you'd use `@`. But in general you just want to handle the error you get, and dont just ignore them. – sroes May 25 '13 at 13:44
  • @migg the code will stop executing at the point of the error and jump to the catch – sroes May 25 '13 at 13:44
  • @sroes But all other errors that are not inside a try-catch will not. – crackmigg May 25 '13 at 13:46
  • @sroes I would advice you to just remove the first sentence. The remaining part of the answer is good – hek2mgl May 25 '13 at 13:46
  • @hek2mgl I did not see the edit of `error_get_last`; didn't know about that function. But for using $php_errormsg: I personally always try to prevent situations where a variable magically appears, but I guess that's just part of PHP. – sroes May 25 '13 at 13:54
  • However, as I said, your proposal is good. But it's more suited for a global attempt, meaning catching all warnings and errors in a well defined way (therefore +1). But for local operations the `@` operator is still important and handy.. you'll see ;) .... I just wanted to point that both solutions have there advantages and disadvantages and neither of them is *wrong* – hek2mgl May 25 '13 at 13:57
  • 1
    Yeah, you're right. It's not wrong to use it, it just depends on opinion and the context where it's used. – sroes May 25 '13 at 14:07
2

This should work:

@file_get_contents("http://www.site.com");

The @ suppresses warnings and errors to be output by PHP. You will have to deal with an empty response yourself then.

crackmigg
  • 5,571
  • 2
  • 30
  • 40
1

You can use curl for avoid displaying php errors :

    $externalUrl = ** your http request **

    curl_setopt($curl, CURLOPT_URL, $externalUrl); // Set the URL
    curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36'); // Use your user agent
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // Set so curl_exec returns the result instead of outputting it.
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // Bypass SSL Verifyers
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($curl, CURLOPT_TIMEOUT, 10);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/x-www-form-urlencoded'
    ));

    $result = curl_exec($curl); // send request
    $result = json_decode($result);
0

You can turn off warnings in PHP :

Turn off warnings and errors on php/mysql

See in documentation:

http://il1.php.net/manual/en/function.file-get-contents.php

Return Values: The function returns the read data or FALSE on failure.

or write @, before function, for avoid to see error:

@file_get_contents(...

Community
  • 1
  • 1
CETb
  • 344
  • 1
  • 3
  • 11
  • Hmm, but errors are always good way to handle whats the problem, only this one is disgusting for me. – user198989 May 25 '13 at 13:35
  • If fail request is can be many problems. Better way to work over PHP extension - CURL, that if you want to know exactly what's happen, after request. - Can be DNS request fail - Wrong page 404, 501 - Server timeout. Anyway you receive FALSE, if request is wrong. Not sure, if it need to do over exception.. Better anyway in production server to disable warnings. – CETb May 25 '13 at 19:07