2013-02-26 86 views
2

我有一個需要傳遞的XML文件。解析JSON字符串(還是XML?)

http://mckay.canvashost.com:8080/opentripplanner-api-webapp/ws/transit/stopTimesForStop?id=27833&startTime=1361784386000&extended=true&references=true

我使用PHP的捲曲將其加載到頁面:

$url = "http://mckay.canvashost.com:8080/opentripplanner-api-webapp/ws/transit/stopTimesForStop?id=27833&startTime=1361784386000&extended=true&references=true"; 
$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
$xml = curl_exec($ch);  
curl_close($ch); 
echo htmlentities($xml, ENT_QUOTES, "UTF-8"); 

輸出是一個JSON文件,我沒想到。無論如何,我繼續使用JSON工作,但似乎無法解析它。

我原以爲這會工作:

$obj=json_decode($xml); 
echo $obj[stopTime][0][phase]; 

隨着 '出發' 的結果。我在這方面花了很長時間,但卻無法輸出任何內容。

任何幫助?任何人都知道它從XML文件切換到JSON格式?

+0

你到底把解壓出來的這個數據? – Blender 2013-02-26 04:03:56

+2

試試'json_decode($ xml,true);'。 [PHP文檔](http://www.php.net/manual/en/function.json-decode.php)。 – Passerby 2013-02-26 04:08:15

+0

該鏈接爲我返回一個XML文件。是什麼讓你認爲它是JSON? – cdmckay 2013-02-26 04:20:52

回答

0

輸出是一個xml字符串。 嘗試使用由SimpleXML的交付PHP的XML功能:

SimpleXml

只要你成功解析字符串中,你可以遍歷其節點像數組

foreach ($xmlElement as $node) { 
echo $node->attributeName; 
}