2011-11-16 78 views
0

我希望能够检测误差的SimpleXMLElement,错误是:使用SimpleXMLElement检测错误?

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

我有以下代码尝试检测错误。

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

这不起作用....我如何使错误检测工作?

回答

1

异常可以使用try抓建设被抓:

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

对于HTTP /负载问题,我建议你使用的组合:

  1. CURL检查有效的响应
  2. simplexml_load_string 。解答回复,并使用libxml_use_internal_errors