Thursday, March 25, 2010

PHP: file_get_contents catch exception

Script has been modified and provides a neat example, look here: file_get_contents() exception handling improved

Many PHP users are looking for a way to catch the exception from file_get_contents() function. I've had my share of trying to catch one too but the amount of code required to do so is above my level. After countless hours of wasted time I decided to simply ignore the error, like this:

$a = @file_get_contents($url);

@ - simply ignores the error if there is one. Nothing will happen, no output will come up.

Recently, I had to use file_get_contents() function once again while appying object-oriented programming and a thought came to my head. I went to php.net website and checked how file_get_contents() works, again.

This is what PHP manual says:

" file_get_contents() - returns a file in a string." (We all know that)
" On failure, file_get_contents() will return FALSE. " - there is the answer.

If the function returns false, that means something went wrong and the result is not what you expected, so the simplest way to check if the function returned anything or not without handling/catching the exception would be to test the function in the IF statement, like this:

if ( file_get_contents( $url ) != false ) {
$a = file_get_contents($url);
.... whatever it is that you wanted to do with the $a string
}

Technically you are running the function twice, but this will ensure it works or not without having to go about catching exceptions.

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. For those of you that want to use exception handling then you can take this approach which will increase performance by not having to call the file_get_contents() function twice:

    $result = @file_get_contents($url);
    try
    {

    if ( $result == FALSE )
    {
    throw new Exception ("Failed to get page contents.");
    }

    }

    catch(Exception $ex)
    {
    echo "Error: ".$ex->getMessage();
    }

    Lets go over the code. We placed the code that might raise any error in the 'try' block. By placing the '@' sign in front of the function you suppress (ignore) any errors the function may raise and store the result into $result variable. Now, our simply 'if' statement checks if the $result variable is set to FALSE meaning there was an error in returning page contents and the program is terminated.

    We use the 'catch' block to work with the the error and to display a normal sentence to the user instead of programming code. Keep in mind that in this example I used a general exception catch block for example purposes you can use a more detailed one if you want.

    ReplyDelete
  3. Why would you want to call the fetch twice? That's VERY expensive. Just all it once and check the return value as follows:

    if (($a = file_get_contents( $url )) === false ) {
    echo "some error";
    }else{
    .... whatever it is that you wanted to do with the $a string
    }

    This is important, you will also want to use === or !== instead of just == or != in this case. For cleaner coding I usually prefer "=== false" so there is no doubt as to the validity of the returned value types.

    ReplyDelete