开发者社区> 问答> 正文

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;  
              
        }  
    }  
    ?>  

展开
收起
落地花开啦 2016-06-13 16:15:56 2176 0
1 条回答
写回答
取消 提交回答
  • 喜欢技术,喜欢努力的人

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

    2019-07-17 19:36:19
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
阿里云栖开发者沙龙PHP技术专场-直面PHP微服务架构挑战-高驰涛 立即下载
PHP安全开发:从白帽角度做安全 立即下载
PHP 2017.北京 全球开发者大会——高可用的PHP 立即下载