如何使用PHP调用API接口实现天气查询功能

如题所述

最近在做微信公众平台测试时,想在里面子菜单上添加查询未来几天(包括今天)天气的功能,就查找了下好用的天气预报查询接口API,使用比较多的有:国家气象局天气接口、新浪天气预报接口、百度天气预报接口、google天气接口、Yahoo天气接口等等,我使用的是百度提供的免费天气查询接口API,下面与大家分享下...

1、查询方式:

百度提供的是根据纬度和城市名查询天气情况

2、接口事例:

3、接口参数说明:


4、返回结果说明:


5、

    //城市名  

    $city = '上海';  

    //对json格式的字符串进行编码  

    $arr =json_decode($str,TRUE);         

    print_r($atr);  

    //城市名
       $city = '上海';
     
       //获取json格式的数据
       $str =file_get_contents("http://api.map.baidu.com/telematics/v3/weather?location=".$city."&output=json&ak=5slgyqGDENN7Sy7pw29IUvrZ");

       //对json格式的字符串进行编码
       $arr =json_decode($str,TRUE);      

       print_r($atr);


    6、返回页面的是json编码后的数据:

    [plain] view plain copy print?

    <meta charset="UTF-8">  

    Array  

    (  

    [error] => 0  

    [status] => success  

    [date] => 2014-03-17  

    [results] => Array  

    (  

    [0] => Array  

    (  

    [currentCity]=> ä¸Šæµ·  

    [weather_data]=> Array  

    (  

    [0]=> Array  

    (  

    [date] => å‘¨ä¸€(今天, å®žæ—¶ï¼š19℃)  

    [dayPictureUrl] =>http://api.map.baidu.com/images/weather/day/qing.png  

    [nightPictureUrl] =>http://api.map.baidu.com/images/weather/night/qing.png  

    [weather] => æ™´  

    [wind] => è¥¿å—风3-4级  

    [temperature] => 13℃  

    )  

    [1] => Array  

    (  

    [date]=> å‘¨äºŒ  

    [dayPictureUrl] =>http://api.map.baidu.com/images/weather/day/duoyun.png  

    [nightPictureUrl] => http://api.map.baidu.com/images/weather/night/yin.png  

    [weather]=> å¤šäº‘转阴  

    [wind]=> ä¸œåŒ—风3-4级  

    [temperature] => 24 ~ 9℃  

    )  

    [2] => Array  

    (  

    [date]=> å‘¨ä¸‰  

    [dayPictureUrl] =>http://api.map.baidu.com/images/weather/day/zhongyu.png  

    [nightPictureUrl] => http://api.map.baidu.com/images/weather/night/xiaoyu.png  

    [weather]=> ä¸­é›¨è½¬å°é›¨  

    [wind]=> ä¸œåŒ—风3-4级  

    [temperature] => 15 ~ 8℃  

    )  

    [3] => Array  

    (  

    [date]=> å‘¨å››  

    [dayPictureUrl] =>http://api.map.baidu.com/images/weather/day/duoyun.png  

    [nightPictureUrl] =>http://api.map.baidu.com/images/weather/night/qing.png  

    [weather]=> å¤šäº‘转晴  

    [wind]=> åŒ—风3-4级  

    [temperature] => 14 ~ 6℃  

    )  

    )  

    )  

    )  

    )  

    <meta charset="UTF-8">
    Array
    (
       [error] => 0
       [status] => success
       [date] => 2014-03-17
       [results] => Array
           (
               [0] => Array
                   (
                       [currentCity]=> 上海
                       [weather_data]=> Array
                           (
                               [0]=> Array
                                   (
                                      [date] => 周一(今天, 实时:19℃)
                                      [dayPictureUrl] =>http://api.map.baidu.com/images/weather/day/qing.png
                                      [nightPictureUrl] =>http://api.map.baidu.com/images/weather/night/qing.png
                                       [weather] => 晴
                                      [wind] => 西南风3-4级
                                      [temperature] => 13℃
                                   )

                               [1] => Array
                                   (
                                       [date]=> 周二
                                      [dayPictureUrl] =>http://api.map.baidu.com/images/weather/day/duoyun.png
                                      [nightPictureUrl] => http://api.map.baidu.com/images/weather/night/yin.png
                                       [weather]=> 多云转阴
                                       [wind]=> 东北风3-4级
                                      [temperature] => 24 ~ 9℃
                                   )

                               [2] => Array
                                   (
                                       [date]=> 周三
                                      [dayPictureUrl] =>http://api.map.baidu.com/images/weather/day/zhongyu.png
                                      [nightPictureUrl] => http://api.map.baidu.com/images/weather/night/xiaoyu.png
                                       [weather]=> 中雨转小雨
                                       [wind]=> 东北风3-4级
                                      [temperature] => 15 ~ 8℃
                                   )

                               [3] => Array
                                   (
                                       [date]=> 周四
                                      [dayPictureUrl] =>http://api.map.baidu.com/images/weather/day/duoyun.png
                                       [nightPictureUrl] =>http://api.map.baidu.com/images/weather/night/qing.png
                                       [weather]=> 多云转晴
                                       [wind]=> 北风3-4级
                                      [temperature] => 14 ~ 6℃
                                   )

                           )

                   )

           )

    )

    7、PHP中自带了处理json格式字符串的内置函数,下面做一个事例,并给出完整代码:

    [php] view plain copy print?

    <metacharset="UTF-8">  

    <?php  

    //城市名  

    $city = '上海';  

    //获取json格式的数据  

    $str = file_get_contents("http://api.map.baidu.com/telematics/v3/weather?location=".$city."&output=json&ak=5slgyqGDENN7Sy7pw29IUvrZ");  

    //对json格式的字符串进行编码  

    $arr = json_decode($str,TRUE);         

    echo "城市:".$arr['results'][0]['currentCity']." æ—¥æœŸï¼š".$arr['date']."<br /><br />";  

    foreach($arr['results'][0]['weather_data']as $val)  

    {  

    echo $val['date']."<br/>";  

    echo "天气:{$val['weather']}<br/>";  

    echo "风向:{$val['wind']}<br/>";  

    echo "温度:{$val['temperature']}<br/><br />";  

    }  

    ?>  

    <metacharset="UTF-8">
    <?php
       //城市名
       $city = '上海';
     
       //获取json格式的数据
       $str = file_get_contents("http://api.map.baidu.com/telematics/v3/weather?location=".$city."&output=json&ak=5slgyqGDENN7Sy7pw29IUvrZ");

       //对json格式的字符串进行编码
       $arr = json_decode($str,TRUE);      

       echo "城市:".$arr['results'][0]['currentCity']." 日期:".$arr['date']."<br /><br />";

       foreach($arr['results'][0]['weather_data']as $val)
       {
           echo $val['date']."<br/>";
           echo "天气:{$val['weather']}<br/>";
           echo "风向:{$val['wind']}<br/>";
           echo "温度:{$val['temperature']}<br/><br />";
       }
    ?>


    8、返回的内容如下:


温馨提示:答案为网友推荐,仅供参考
相似回答