Catching PHP errors if XML file is empty

error-handling, php, xml

Solution

$xml = file_get_contents("http://myurl.blah");
if (trim($xml) == '') {
    die('No content');
}

$xml = simplexml_load_string($xml);

Or, possibly slightly more efficient, but not necessarily recommended because it silences errors:

$xml = @simplexml_load_file($url);
if (!$xml) {
    die('error');
}

Problem

so I'm grabbing some information from an XML file like so: ``` $url = "http://myurl.blah"; $xml = simplexml_load_file($url); ``` Except sometimes the XML file is empty and I need the code to fail gracefully but I can't seem to figure out how to catch the PHP error. I tried this: ``` if(isset(simplexml_load_file($url))); { $xml = simplexml_load_file($url); /*rest of code using $xml*/ } else { echo "No info avilable."; } ``` But it doesn't work. I guess you can't use ISSET that way. Anyone know how to catch the error?

Original source