search
Homephp教程PHP源码解析雅虎天气API返回的XML数据

使用XMLReader解析

$url = 'http://weather.yahooapis.com/forecastrss?w=15015432&u=c';
$reader = new XMLReader();
$reader->open($url,'utf-8');
while($reader->read()){
    if($reader->name == 'yweather:condition'){
        $code = $reader->getAttribute('code');   //获取天气代码
        $temp = $reader->getAttribute('temp');   //获取温度
    }
    if($reader->name == 'yweather:atmosphere'){
        $humi = $reader->getAttribute('humidity');   //获取湿度
    }
}

使用DOMDocument解析

$url = 'http://weather.yahooapis.com/forecastrss?w=15015432&u=c';
$yweather = "http://xml.weather.yahoo.com/ns/rss/1.0";  //命名空间
$res = new DOMDocument();
$res->load($url);
 
$node = $res->getElementsByTagNameNS($yweather, 'atmosphere');
$humi = $node->item(0)->attributes->item(0)->nodeValue;     //获取湿度
$node = $res->getElementsByTagNameNS($yweather, 'condition');
$code = $node->item(0)->attributes->item(1)->nodeValue;     //获取天气代码
$temp = $node->item(0)->attributes->item(2)->nodeValue;     //获取温度

将数字格式的天气代码转换成相应的中文

function code2char($code){
    switch($code){
        case 0:
            return '龙卷风';
        case 1:
            return '热带风暴';
        case 2:
            return '暴风';
        case 3:
            return '大雷雨';
        case 4:
            return '雷阵雨';
        case 5:
            return '雨夹雪';
        case 6:
            return '雨夹雹';
        case 7:
            return '雪夹雹';
        case 8:
            return '冻雾雨';
        case 9:
            return '细雨';
        case 10:
            return '冻雨';
        case 11:
            return '阵雨';
        case 12:
            return '阵雨';
        case 13:
            return '阵雪';
        case 14:
            return '小阵雪';
        case 15:
            return '高吹雪';
        case 16:
            return '雪';
        case 17:
            return '冰雹';
        case 18:
            return '雨淞';
        case 19:
            return '粉尘';
        case 20:
            return '雾';
        case 21:
            return '薄雾';
        case 22:
            return '烟雾';
        case 23:
            return '大风';
        case 24:
            return '风';
        case 25:
            return '冷';
        case 26:
            return '阴';
        case 27:
            return '多云';
        case 28:
            return '多云';
        case 29:
            return '局部多云';
        case 30:
            return '局部多云';
        case 31:
            return '晴';
        case 32:
            return '晴';
        case 33:
            return '转晴';
        case 34:
            return '转晴';
        case 35:
            return '雨夹冰雹';
        case 36:
            return '热';
        case 37:
            return '局部雷雨';
        case 38:
            return '偶有雷雨';
        case 39:
            return '偶有雷雨';
        case 40:
            return '偶有阵雨';
        case 41:
            return '大雪';
        case 42:
            return '零星阵雪';
        case 43:
            return '大雪';
        case 44:
            return '局部多云';
        case 45:
            return '雷阵雨';
        case 46:
            return '阵雪';
        case 47:
            return '局部雷阵雨';
        default:
            return '水深火热';
    }
Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.