登录

xml - php调用新浪天气API遇到的一个问题

调用新浪天气的API(xml格式返回),可以得到xml字符串,可是装载到对象中后却输出不出来,是怎么回事?

    <?php   
    /** 
    *  
    */  
    $w = new Weather();  
    echo $w->getWeather();  
    class Weather   
    {  
        private $apiUrl = 'http://php.weather.sina.com.cn/xml.php?city=%B1%B1%BE%A9&password=DJOYnieT8234jlsK&day=0';  
        public function getWeather()  
        {  
            $xml = file_get_contents($this->apiUrl);  
            $result = simplexml_load_string($xml);  
            $weather = $result->pollution;//xml中的一个子节点  
            return $weather;  
              
        }  
    }  
    ?>  
# PHP
大家讲道理大家讲道理2198 天前708 次浏览

全部回复(1) 我要回复

  • 天蓬老师

    天蓬老师2017-04-10 14:27:09

    $weather = $result->pollution;//xml中的一个子节点
    修改为:
    $weather = $result->Weather->pollution;

    回复
    0
  • 取消回复发送