2017-08-26 58 views
1

正如你所看到的我已經在Stackoverflow上檢查了一些其他的答案,但我只得到空白的結果。這是我到目前爲止:PHP simplexml_load_file不解析

$city = simplexml_load_file('http://api.openweathermap.org/data/2.5/weather?q=Tijuana&mode=xml&appid=12fcd235af3a27a895aecb26bc957055'); 

echo $city['current']['city']['name']; 
echo $city->current->city; 

任何幫助,非常感謝。

+1

試試這個'回聲(字符串)$城市 - >城市[ 「名稱」];''simplexml_load_string'或'file'忽略根元素,它是'你的情況 –

+0

最後一個問題current':薩赫勒,我可以得到第一個口袋的結果,但不是第二個...例如: <?php $ base_clima = simplexml_load_file('http://api.openweathermap.org/data/2.5/weather?q=Tijuana&mode = XML&的appid = 12fcd235af3a27a895aecb26bc957055' ); // print_r($ city); $ humedad =(string)$ base_clima-> humidity [「value」]; $ wind_v =(string)$ base_clima-> wind [「speed」] ['value']; >

<= '溼度:'???$ humedad '%'>

<= '風速:'。 $ wind_v .'Km/h'?>

+0

我不明白哪個第二口袋? –

回答

1

在你的代碼更改此:

$base_clima->wind["speed"]['value']; 

此:對象的

$base_clima->wind->speed['value']; 

樣本打印:

[wind] => SimpleXMLElement Object 
     (
      [speed] => SimpleXMLElement Object 
       (
        [@attributes] => Array 
         (
          [value] => 1.95 
          [name] => Light breeze 
         ) 

       ) 
     ) 

用於訪問對象的元素你可以e像這樣$object->element1->element2但在simplexml_load_string/file@attributes可以作爲一個數組訪問,所以你可以像這樣使用它。

試試這個,希望這個會有幫助。

$city = simplexml_load_file('http://api.openweathermap.org/data/2.5/weather?q=Tijuana&mode=xml&appid=12fcd235af3a27a895aecb26bc957055'); 
echo $humedad = (string)$city->humidity["value"]; 
echo $wind_v = (string)$city->wind->speed['value']; 
+1

你是一個屬! 非常感謝。 –

相關問題