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);