2017-08-26 75 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

你是一个属! 非常感谢。 –