01jayss
01jayss

Reputation: 1449

Detect Error with SimpleXMLElement?

I want to be able to detect error with SimpleXMLElement, the errors being:

failed to open HTTP stream
failed to load external entity
Uncaught exception 'Exception' with message 'String could not be parsed as XML' 

I have the following code to attempt to detect error.

$xml =  new SimpleXMLElement("https://mail.google.com/mail/feed/atom",null,true);
if ($xml === FALSE) {
echo "Wrong username/password combination.";
}

This doesn't work.... how do I make error detection work?

Upvotes: 0

Views: 608

Answers (1)

Wesley van Opdorp
Wesley van Opdorp

Reputation: 14941

Exceptions can be caught using a try catch construction:

try {
     $oXml 
}
catch(Excepton $oException) {
   // Something went wrong!
}

For the HTTP / Load problems I suggest you use a combination of:

  1. CURL to check valid response
  2. simplexml_load_string.parse the response, and catch any warnings using libxml_use_internal_errors

Upvotes: 1

Related Questions