检查响应php中的xml是否无效
我通过curl发出一个通常的请求来获取一个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);
$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()
根据需要获取错误信息