8

I am trying to fetch an image using file_get_contents function but it gives an error. To handle the error I am using try catch block but it does not catch the error and fails.

My code:

try {
     $url = 'http://wxdex.ocm/pdd.jpg'; //dummy url
     $file_content = file_get_contents($url);
}
catch(Exception $e) {
     echo 'Error Caught';           
}

Error:

Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: No such host is known
Warning: file_get_contents(http://wxdex.ocm/pdd.jpg): failed to open stream: php_network_getaddresses: getaddrinfo failed: No such host is known.

NOTE:: I am able to fetch any other valid image url on remote.

D555
  • 1,704
  • 6
  • 26
  • 48

3 Answers3

14

try/catch doesn't work because a warning is not an exception.

You can try this code so you can catch warnings as well.

//set your own error handler before the call
set_error_handler(function ($err_severity, $err_msg, $err_file, $err_line, array $err_context)
{
    throw new ErrorException( $err_msg, 0, $err_severity, $err_file, $err_line );
}, E_WARNING);

try {
    $url = 'http://wxdex.ocm/pdd.jpg';
    $file_content = file_get_contents($url);
} catch (Exception $e) {
    echo 'Error Caught'; 
}

//restore the previous error handler
restore_error_handler();
Edison Biba
  • 4,384
  • 3
  • 17
  • 33
  • 1
    Be cautious when automagically converting warnings to exceptions: any minor issue will abort the whole program if you don't explicitly catch it at the right point. Also, `set_error_handler()` overrides any previous handler you might have defined. – Álvaro González Jun 29 '18 at 07:34
  • 3
    @ÁlvaroGonzález that's why i have used restore_error_handler() after that – Edison Biba Jun 29 '18 at 07:36
  • 1
    super fix.. It helped me lot – Ramesh_D Jan 16 '19 at 14:50
1

Following is the altenative way , just need to check for the data , if not we can throw the exception to handle it. it will be safer compared with setting the new error handler

try {
    $url = 'http://wxdex.ocm/pdd.jpg';
    $file_content = file_get_contents($url);
    if(empty($file_content)){
       throw new Exception("failed to open stream ", 1);
    }else{
       echo "File is loaded and content is there";
     }

} catch (Exception $e) {
   echo 'Error Caught'; 
}
Mahesh Hegde
  • 1,131
  • 10
  • 12
  • Unfortunately this does not work. It throws a warning, so I had to use @ to suppress the warning. Thanks though. – D555 Jun 30 '18 at 04:09
  • it returns the same error originally displayed in the browser ... at least it helps the application not to stop due to the error ... – Pathros Sep 21 '22 at 18:51
0

check URL exist before that using get header function

$url = 'http://wxdex.ocm/pdd.jpg';
$file_headers = @get_headers($url);

if(!$file_headers || $file_headers[0] == 'HTTP/1.1 404 Not Found' ||trim($file_headers[0]) == 'HTTP/1.1 403 Forbidden') {
    $exists = false;
}else{
    $exists = true;
}
if($exists===true){

    $file_content = file_get_contents($url);
}
  • this is not good solution as `@get_headers()` just silencing errors or warning. we can straight away use `@file_get_contents()` to see if that returns any value. – WBGUY001 May 19 '20 at 13:06