首页 php教程 PHP源码 解析雅虎天气API返回的XML数据

解析雅虎天气API返回的XML数据

May 25, 2016 pm 05:06 PM

使用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 '水深火热';
    }
登录后复制
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)