PHP DOMDocument 错误处理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1759069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP DOMDocument error handling
提问by Maksim Vi.
In my application I am loading xml from url in order to parse it. But sometimes this url may not be valid. In this case I need to handle errors. I have the following code:
在我的应用程序中,我从 url 加载 xml 以解析它。但有时这个网址可能无效。在这种情况下,我需要处理错误。我有以下代码:
$xdoc = new DOMDocument();
try{
$xdoc->load($url); // This line causes Warning: DOMDocument::load(...)
// [domdocument.load]: failed to open stream:
// HTTP request failed! HTTP/1.1 404 Not Found in ...
} catch (Exception $e) {
$xdoc = null;
}
if($xdoc == null){
// Handle
} else {
// Proceed
}
I know I probably doing it wrong, but what's a correct way to handle this kind of exceptions? I don't want to see error messages on my page.
我知道我可能做错了,但是处理这种异常的正确方法是什么?我不想在我的页面上看到错误消息。
The manual for DOMDocument::load() says:
DOMDocument::load() 手册说:
If an empty string is passed as the filename or an empty file is named, a warning will be generated. This warning is not generated by libxml and cannot be handled using libxml's error handling functions.
如果将空字符串作为文件名传递或命名为空文件,则会生成警告。此警告不是由 libxml 生成的,无法使用 libxml 的错误处理函数进行处理。
But there is no information on how to handle it.
但是没有关于如何处理它的信息。
Thanks.
谢谢。
回答by ?ystein Riiser Gundersen
From what I can gather from the documentation, handling warnings issued by this method is tricky because they are not generated by the libxml extension and thus cannot be handled by libxml_get_last_error(). You could either use the error suppression operator and check the return value for false...
根据我从文档中收集到的信息,处理此方法发出的警告很棘手,因为它们不是由 libxml 扩展生成的,因此无法由libxml_get_last_error(). 你既可以使用错误抑制操作和检查返回值false...
if (@$xdoc->load($url) === false)
// ...handle it
...or register an error handler which throws an exception on error:
...或注册一个错误处理程序,该处理程序在错误时引发异常:
function exception_error_handler($errno, $errstr, $errfile, $errline ) {
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
and then catch it.
然后抓住它。
回答by Znarkus
set_error_handler(function($number, $error){
if (preg_match('/^DOMDocument::loadXML\(\): (.+)$/', $error, $m) === 1) {
throw new Exception($m[1]);
}
});
$xml = new DOMDocument();
$xml->loadXML($xmlData);
restore_error_handler();
That works for me in PHP 5.3. But if you're not using loadXML, you might need to do some modifications.
这在 PHP 5.3 中对我有用。但是,如果您不使用loadXML,则可能需要进行一些修改。
回答by Petah
To disable throwing errors:
要禁用抛出错误:
$internal_errors = libxml_use_internal_errors(true);
$dom = new DOMDocument();
// etc...
libxml_use_internal_errors($internal_errors);
回答by Galen
From php.net
来自 php.net
If an empty string is passed as the filename or an empty file is named, a warning will be generated. This warning is not generated by libxml and cannot be handled using libxml's error handling functions.
如果将空字符串作为文件名传递或命名为空文件,则会生成警告。此警告不是由 libxml 生成的,无法使用 libxml 的错误处理函数进行处理。
In your production environment you shouldn't have errors displayed to the user. They don't need to see them so taking this into account you can use...
在您的生产环境中,您不应该向用户显示错误。他们不需要看到它们,因此考虑到这一点,您可以使用...
$xdoc = new DOMDocument();
if ( $xdoc->load($url) ) {
// valid
}
else {
// invalid
}
回答by sumit
For me , following did the trick
对我来说,以下是诀窍
$feed = new DOMDocument();
$res= @$feed->load('http://www.astrology.com/horoscopes/daily-extended.rss');
if($res==1){
//do sth
}

