Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查响应php中的xml是否无效_Php_Xml - Fatal编程技术网

检查响应php中的xml是否无效

检查响应php中的xml是否无效,php,xml,Php,Xml,我通过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发出一个通常的请求来获取一个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\u load\u string($output)

但是我遇到了一个具有无效xml结构的url,因此当我使用
simplexml\u load\u string
函数时,它会出现错误,并且尝试在浏览器中打开时,它会显示这一点

This page contains the following errors:

error on line 3 at column 53: invalid character in attribute value
因此,我想尝试检查响应是否包含该错误,以便在代码中执行必要的操作

我试过这样的smth

if (strpos($output, "This page contains the following errors:") === false) {
    echo "valid xml";
} else {
    echo "invalid xml structure";
}
但它不起作用,即使url返回无效的xml,毕竟它在响应中也找不到该文本


谢谢

您可以使用
libxml\u use\u internal\u errors(true)
关闭错误,并使用
libxml\u get\u errors()
根据需要获取错误信息