Php error handling

I have a file that opens a url and reads it and parses it. Now if that url goes dwon and my file doesn't open, then I need the error message to be generated, but no error message appears in the terminal or console. How can i do this? Plz help!

+1


a source to share


3 answers


You can always do something like this (I assume you are using file_get_contents)



$file = @fopen("abc.com","rb");
if(!$file) { 
    @mail(.......); 
    die(); 
} 
//rest of code. Else is not needed since script will die if hit if condition

      

+2


a source


Use curl instead if you are downloading files over the network. It has error handling built in, and it will report the error to you. Using file_get_contents won't tell you what went wrong, it won't follow redirects either.



$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://domain.com/file');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch);
if ( $result == false ) {
    $errorInfo = curl_errno($ch).' '.curl_error($ch);
    mail(...)
} else {
    //Process file, $result contains file contents
}

      

+1


a source


if (!$content = file_get_contents('http://example.org')) {
  mail(...);
}

      

0


a source







All Articles