您当前的位置:首页 > 美文摘抄 > 内容

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

并给出完整代码:[php] view plain copy print?《metacharset=“UTF-8“》  《?php  //城市名  $city = ’上海’;  //获取json格式的数据  $str = file_get_contents(“《?php   //城市名   $city = ’上海’;    //获取json格式的数据   $str = file_get_contents(“   //对json格式的字符串进行编码   $arr = json_decode($str,TRUE);         echo “城市:“.$arr[’results’][’currentCity’].“ 日期:“.$arr[’date’].“《br /》《br /》“;   foreach($arr[’results’][’weather_data’]as $val)   {       echo $val[’date’].“《br/》“;       echo “天气:{$val[’weather’]}《br/》“;       echo “风向:{$val[’wind’]}《br/》“;       echo “温度:{$val[’temperature’]}《br/》《br /》“;   }?》8、返回的内容如下:彩云天气API怎么获取腾讯位置服务平台提供获取经纬度的接口彩云天气申请开发者api调用对应经纬度的天气状况测试环境选择了自己的iphone上的JSbox来运行一个简单的js脚本://简单思路就是 获取ip再获取天气信息const locationKey = “XXXXXXXXXXXXX“const weatherKey = “XXXXXXXXXXXX“const apiList = { location:“}function getLonLat(){ $ url: `${apiList.location}/location/v1/ip?key=${locationKey}`, handler: (resp) =》 { let location = resp.data&&resp.data.result&&resp.data.result.location getLocation(location) } });}function getLocation(location){ $ url: `${apiList.location}/geocoder/v1/?key=${locationKey}&location=${location.lat},${location.lng}`, handler: (resp) =》 { var data = resp.data; $console.info(data.result.formatted_addresses.recommend); } });}/** * * @param {lat:““,lng:““} location */function getWeather(location){ $ url: `${apiList.weather}/${weatherKey}/${location.lng},${location.lat}/weather.json`, handler: (resp) =》 { let data = resp.data; console.info(data) //运行结果参照彩云天气 } });}getLonLat()2|0顿时醒悟写到这其实我只是想测试一下两个接口的基本用法以及可用之处,而墨迹赤必提供的API接口就是这样一个作用,参考资料:百度百科-api接口墨迹赤必提供的天气预报API接口有什么作用这块东西往专业来说肯定大部分人不是太了解,然后突然想到jsbox里面内置的$location可以直接获取到设备的位置信息,通过这样获取到的位置坐标会比ip的更加精准//根据原生SDK获取手机位置function getPhoneLoc(){ $location.fetch({ handler: function(resp) { var lat = resp.lat; var lng = resp.lng; var alt = resp.alt; let loc = {lat:lat,lng:lng} getLocation(loc) } });}3|0最后运行结果如何使用百度天气预报API接口百度API Key申请地址: API函数包含在Windows系统目录下的动态连接库文件中,而墨迹赤必的API接口就起着这样的作用,下面与大家分享下...1、查询方式:百度提供的是根据纬度和城市名查询天气情况2、接口事例:3、接口参数说明:4、返回结果说明:5、//城市名  $city = ’上海’;  //对json格式的字符串进行编码  $arr =json_decode($str,TRUE);         print_r($atr);  //城市名   $city = ’上海’;    //获取json格式的数据   $str =file_get_contents(“   //对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  (   =》 Array  (  [currentCity]=》 上海  [weather_data]=》 Array  (  =》 Array  (  [date] =》 周一(今天, 实时:19℃)  [dayPictureUrl] =》Array(   [error] =》 0   [status] =》 success   [date] =》 2014-03-17   [results] =》 Array       (            =》 Array               (                   [currentCity]=》 上海                   [weather_data]=》 Array                       (                           =》 Array                               (                                  [date] =》 周一(今天, 实时:19℃)                                  [dayPictureUrl] =》                                  [nightPictureUrl] =》                                   [weather] =》 晴                                  [wind] =》 西南风3-4级                                  [temperature] =》 13℃                               )                            =》 Array                               (                                   [date]=》 周二                                  [dayPictureUrl] =》                                  [nightPictureUrl] =》                                    [weather]=》 多云转阴                                   [wind]=》 东北风3-4级                                  [temperature] =》 24 ~ 9℃                               )                            =》 Array                               (                                   [date]=》 周三                                  [dayPictureUrl] =》                                  [nightPictureUrl] =》                                    [weather]=》 中雨转小雨                                   [wind]=》 东北风3-4级                                  [temperature] =》 15 ~ 8℃                               )                            =》 Array                               (                                   [date]=》 周四                                  [dayPictureUrl] =》                                   [nightPictureUrl] =》                                   [weather]=》 多云转晴                                   [wind]=》 北风3-4级                                  [temperature] =》 14 ~ 6℃                               )                       )               )       ))7、PHP中自带了处理json格式字符串的内置函数,如何使用PHP调用API接口实现天气查询功能最近在做微信公众平台测试时,因为墨迹赤必拥有着丰富的天气数据。

如何使用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(“
  •    //对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  

  • (  

  •  =》 Array  

  • (  

  • [currentCity]=》 上海  

  • [weather_data]=》 Array  

  • (  

  • =》 Array  

  • (  

  • [date] =》 周一(今天, 实时:19℃)  

  • [dayPictureUrl] =》

  • Array
  • (
  •    [error] =》 0
  •    [status] =》 success
  •    [date] =》 2014-03-17
  •    [results] =》 Array
  •        (
  •             =》 Array
  •                (
  •                    [currentCity]=》 上海
  •                    [weather_data]=》 Array
  •                        (
  •                            =》 Array
  •                                (
  •                                   [date] =》 周一(今天, 实时:19℃)
  •                                   [dayPictureUrl] =》
  •                                   [nightPictureUrl] =》
  •                                    [weather] =》 晴
  •                                   [wind] =》 西南风3-4级
  •                                   [temperature] =》 13℃
  •                                )
  •                             =》 Array
  •                                (
  •                                    [date]=》 周二
  •                                   [dayPictureUrl] =》
  •                                   [nightPictureUrl] =》
  •                                    [weather]=》 多云转阴
  •                                    [wind]=》 东北风3-4级
  •                                   [temperature] =》 24 ~ 9℃
  •                                )
  •                             =》 Array
  •                                (
  •                                    [date]=》 周三
  •                                   [dayPictureUrl] =》
  •                                   [nightPictureUrl] =》
  •                                    [weather]=》 中雨转小雨
  •                                    [wind]=》 东北风3-4级
  •                                   [temperature] =》 15 ~ 8℃
  •                                )
  •                             =》 Array
  •                                (
  •                                    [date]=》 周四
  •                                   [dayPictureUrl] =》
  •                                    [nightPictureUrl] =》
  •                                    [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(“

  • 《?php
  •    //城市名
  •    $city = ’上海’;
  •  
  •    //获取json格式的数据
  •    $str = file_get_contents(“
  •    //对json格式的字符串进行编码
  •    $arr = json_decode($str,TRUE);      
  •    echo “城市:“.$arr[’results’][’currentCity’].“ 日期:“.$arr[’date’].“《br /》《br /》“;
  •    foreach($arr[’results’][’weather_data’]as $val)
  •    {
  •        echo $val[’date’].“《br/》“;
  •        echo “天气:{$val[’weather’]}《br/》“;
  •        echo “风向:{$val[’wind’]}《br/》“;
  •        echo “温度:{$val[’temperature’]}《br/》《br /》“;
  •    }
  • ?》
  • 8、返回的内容如下:

彩云天气API怎么获取

腾讯位置服务平台提供获取经纬度的接口彩云天气申请开发者api调用对应经纬度的天气状况测试环境选择了自己的iphone上的JSbox来运行一个简单的js脚本://简单思路就是 获取ip再获取天气信息const locationKey = “XXXXXXXXXXXXX“const weatherKey = “XXXXXXXXXXXX“const apiList = { location:“}function getLonLat(){ $ url: `${apiList.location}/location/v1/ip?key=${locationKey}`, handler: (resp) =》 { let location = resp.data&&resp.data.result&&resp.data.result.location getLocation(location) } });}function getLocation(location){ $ url: `${apiList.location}/geocoder/v1/?key=${locationKey}&location=${location.lat},${location.lng}`, handler: (resp) =》 { var data = resp.data; $console.info(data.result.formatted_addresses.recommend); } });}/** * * @param {lat:““,lng:““} location */function getWeather(location){ $ url: `${apiList.weather}/${weatherKey}/${location.lng},${location.lat}/weather.json`, handler: (resp) =》 { let data = resp.data; console.info(data) //运行结果参照彩云天气 } });}getLonLat()2|0顿时醒悟写到这其实我只是想测试一下两个接口的基本用法以及可用之处,然后突然想到jsbox里面内置的$location可以直接获取到设备的位置信息,通过这样获取到的位置坐标会比ip的更加精准//根据原生SDK获取手机位置function getPhoneLoc(){ $location.fetch({ handler: function(resp) { var lat = resp.lat; var lng = resp.lng; var alt = resp.alt; let loc = {lat:lat,lng:lng} getLocation(loc) } });}3|0最后运行结果

如何使用百度天气预报API接口

  • 百度API Key申请地址: 

    API函数包含在Windows系统目录下的动态连接库文件中。Windows API是一套用来控制Windows的各个部件的外观和行为的预先定义的Windows函数。

    参考资料:百度百科-api接口

    墨迹赤必提供的天气预报API接口有什么作用

    这块东西往专业来说肯定大部分人不是太了解,我可以给你举个例子,如何让两个平行的路连起来了?很简单,搭个桥就可以实现两条路的互通,而墨迹赤必提供的API接口就是这样一个作用。因为墨迹赤必拥有着丰富的天气数据,而客户需要的是更加精准的需求,两相结合之后需要有一个连结的东西,而墨迹赤必的API接口就起着这样的作用。我想这样说应该能明白吧。


  • 声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,谢谢。

    上一篇: 辽宁省疫情情况,辽宁省沈阳市疫情怎么样(辽宁通报最新疫情情况)

    下一篇: iphone官网查序列号(iphone官网在哪查序列号)



    推荐阅读

    网站内容来自网络,如有侵权请联系我们,立即删除! | 软文发布 | 粤ICP备2021106084号