1
$rss = new DOMDocument();
$rss->load($url);

如果我期望用户提供任何 rssfeed 链接以获取 xml 文档,并且用户试图提供任何不包含任何 xml 文档的随机链接。它肯定会显示错误或警告。

我怎样才能验证这种情况?

4

3 回答 3

4

用于libxml_use_internal_errors自行处理错误。来自http://php.net/manual/de/function.libxml-use-internal-errors.php的示例

// enable user error handling
libxml_use_internal_errors(true);

// load the document
$doc = new DOMDocument;

if (!$doc->load('file.xml')) {
    foreach (libxml_get_errors() as $error) {
        // handle errors here
    }

    libxml_clear_errors();
}
于 2013-03-29T12:47:27.190 回答
0

您可以使用 load 的返回值来检查错误并抑制输出@

$rss = new DOMDocument();
if (@$rss->load($url) == false) {
    //error...
}
于 2013-03-29T12:43:50.237 回答
0

试试这个:

$errors = array();
set_error_handler(function ($number, $error) use (&$errors) {
    if (preg_match('#^DOMDocument::load\(\):#', $error)) {
        $errors[] = $error;
    }
});

$url = '';
$doc = new DOMDocument();
if ($doc->load($url) === false) {
    // handle errors from $errors
}

restore_error_handler();
于 2013-03-29T13:01:26.670 回答