PHP Curl检测错误的xml

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.

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.