使用PHP检查远程文件是否为格式正确的XML [英] Check if remote file is well-formed XML with PHP

查看:102
本文介绍了使用PHP检查远程文件是否为格式正确的XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PHP驱动的站点,其中包含XML库存提要,该提要可从ASP远程提供(即XML提要url的顺序为:

I have a PHP-driven site that includes an XML stock feed, which is remotely served from ASP (i.e. the XML feed url is of the order: http://remote.com/client.asp).

由于供稿经常不可用(我的意思是网站返回ASP错误),因此我想在包含供稿之前检查供稿是否为格式正确的XML.我通常使用的url_exists函数无法解决问题,因为即使出现错误",URL的确存在.

As the feed is often unavailable (by which I mean the site returns an ASP error) I'd like to check if the feed is well-formed XML before including it. My usual url_exists function doesn't do the trick as of course the URL does exist even when 'erroring'.

TIA.

推荐答案

使用 cURL 获得结果和 simplexml 来检查XML是否为

Use cURL to get the result and simplexml to check if the XML is well-formed.

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://remote.com/client.asp");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
if (simplexml_load_string($output)) {
  // well-formed XML
} else {
  // it isn't
}

这篇关于使用PHP检查远程文件是否为格式正确的XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆