php天气接口api(php获取天气)

网友投稿 301 2023-02-05

本篇文章给大家谈谈php天气接口api,以及php获取天气对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。 今天给各位分享php天气接口api的知识,其中也会对php获取天气进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

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

最近在做微信公众平台测试时php天气接口api,想在里面子菜单上添加查询未来几天(包括今天)天气php天气接口api的功能,就查找php天气接口api了下好用的天气预报查询接口API,使用比较多的有php天气接口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=jsonak=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=jsonak=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=jsonak=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、返回的内容如下:


调用百度天气的api时候跨域的问题

同源的问题php天气接口api,目前网上没有很好的解决百度天气api的方案。个人目前实行的方式是php天气接口api,先将api数据的通过file_get_contents获取JSON字符串php天气接口api,然后再用ajax调用.php函数file_get_contents应对百度天气接口

天气预报API接口是什么?

天气预报API接口是气象服务商为客户提供服务的唯一接口。拿墨迹赤必为例,跟客户充分沟通后了解客户的实际需求,从而对现有的数据进行调整,符合客户的需求形成API接口,让客户接入,这样就形成了一个完整的服务。所以说API接口并不是一个很神秘的东西,只不过是企业和客户之间的一条服务纽带。 关于php天气接口api和php获取天气的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。 php天气接口api的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于php获取天气、php天气接口api的信息别忘了在本站进行查找喔。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:SpringBoot2 Jpa 批量删除功能的实现
下一篇:Java为何需要平衡方法调用与内联
相关文章

 发表评论

暂时没有评论,来抢沙发吧~