0
我发现http://www.ibm.com/developerworks/xml/library/x-xml2jsonphp/,但我不知道如何使用此代码从我的Web服务器获取xml。有任何想法吗?解析位于互联网上的xml文件,并将其转换为json
我发现http://www.ibm.com/developerworks/xml/library/x-xml2jsonphp/,但我不知道如何使用此代码从我的Web服务器获取xml。有任何想法吗?解析位于互联网上的xml文件,并将其转换为json
最简单的方法是用file_get_contents()
$xmlString = file_get_contents('http://www...../file.xml');
如果你想SimpleXML对象,你可以使用simplexml_load_file()
$xml = simplexml_load_file('http://www...../file.xml');
这两种方法都需要allow_url_fopen
启用。如果不是,你可以使用curl - 这更复杂,但也给你更多的灵活性。
$c = curl_init('http://www...../file.xml');
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$xmlString = curl_exec($c);
$error = curl_error($c);
curl_close($c);
if ($error)
die('Error: ' . $error);