3

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?

5
  • 1
    In general, you can use the try {} catch (exception $e) {} pair. Commented Jul 24, 2012 at 15:10
  • 2
    simplexml_load_file() does not throw exceptions. It raises E_WARNING errors. Commented Jul 24, 2012 at 15:12
  • @mellamokb The PHP error informing me that the file is empty. What else? Commented Jul 24, 2012 at 15:12
  • To be exact: Warning: simplexml_load_file() [<a href='function.simplexml-load-file'>function.simplexml-load-file</a>]: myurl.blah:1: parser error : Document is empty in blah... Commented Jul 24, 2012 at 15:14
  • @AzzyDude: Sorry, I didn't realize simplexml_load_file was built-in... you may find the tip helpful on the [documentation page](php.net/simplexml_load_file): Use libxml_use_internal_errors() to suppress all XML errors, and libxml_get_errors() to iterate over them afterwards. Commented Jul 24, 2012 at 15:15

4 Answers 4

11
$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');
}
Sign up to request clarification or add additional context in comments.

Comments

1

Don't use isset here.

// Shutdown errors (I know it's bad)
$xml = @simplexml_load_file($url);

// Check you have fetch a response
if (false !== $xml); {
    //rest of code using $xml
} else {
    echo "No info avilable.";
}

6 Comments

Don't use empty here, if ($xml) will do just fine.
Hmm, false !== is pretty ugly as well IMO, but better than empty.
@deceze Why do you think it's ugly?
Yoda conditions are part of a lot framework coding standards (ZF, Symfony, Doctrine, etc). It really prevents being fooled by if ($xml = /* something */) {
I trust that if ($xml) will always be true if $xml is a SimpleXML object and false otherwise...
|
1
if (($xml = simplexml_load_file($url)) !== false) {
  // Everything is OK. Use $xml object.
} else {
  // Something has gone wrong!
}

Comments

0

From PHP manual, error handling (click here):

var_dump(libxml_use_internal_errors(true));

// load the document
$doc = new DOMDocument;

if (!$doc->load('file.xml')) {
    foreach (libxml_get_errors() as $error) {
        // handle errors here
    }

    libxml_clear_errors();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.