2

我通常通过 curl 请求获取 xml 文件

$userAgent = 'Mozilla/5.0 (Windows NT 6.1; rv:21.0) Gecko/20100101 Firefox/21.0';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $requestUrl);
        curl_setopt($ch, CURLOPT_REFERER, $requestUrl);
        curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
        $output = curl_exec($ch);
        curl_close($ch);

然后我用

$xml = simplexml_load_string($output);

但是我遇到了一个具有无效 xml 结构的 url,因此当我使用simplexml_load_string函数时它会出错,并试图在浏览器中打开它会显示这个

This page contains the following errors:

error on line 3 at column 53: invalid character in attribute value

所以,我想尝试检查响应是否包含该错误,以便我可以在我的代码中做必要的事情

我试过这样

if (strpos($output, "This page contains the following errors:") === false) {
    echo "valid xml";
} else {
    echo "invalid xml structure";
}

但它不起作用,即使 url 返回无效的 xml ,毕竟它无法在响应中找到该文本。

谢谢

4

1 回答 1

1

您可以使用libxml_use_internal_errors(true)关闭错误并libxml_get_errors()根据需要使用 来获取错误信息。

http://php.net/manual/en/function.libxml-use-internal-errors.php

于 2013-06-10T21:29:16.287 回答