Home > Backend Development > PHP Tutorial > xml - php调用新浪天气API遇到的一个问题

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 20:50:39
Original
1222 people have browsed it

调用新浪天气的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;  
              
        }  
    }  
    ?>  
Copy after login
Copy after login

回复内容:

调用新浪天气的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;  
              
        }  
    }  
    ?>  
Copy after login
Copy after login

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

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template