0

I have been experiencing some bad returns on xml that I get from another server. I'd like to add some reporting on this for myself but I need to find a sure fire way to identify when simple xml fails.

The errors happen on this specific point $xmlResponse = new SimpleXMLElement( $response );

so basically if there is SimpleXMLElement->__construct error trigger i want to detect this and fire my reporting.

E.g.

if($xmlResponse === Fail){
 // Do reporting
}

any ideas appreciated. I also cant rely on http responses as the response code could be fine but there might be issues with the xml itself.

1

1 Answer 1

0

if you just want to check if the xml is malformed,

if(!(@DOMDocument::loadXML($xml))){
  // do reporting
}
  • funfact: pretty much the only difference between DOMDocument's loadXML and loadHTML is that loadHTML accepts malformed xml, while loadXML does not.
Sign up to request clarification or add additional context in comments.

2 Comments

Will this work even if its a background process run by ajax though?
@DCdaz PHP doesn't care if its invoked by cli, page load, ajax call, or anything else. yes, it will work

Your Answer

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