之前,和大家分享了定时推送。今天来分享,如何推送天气情况,其他信息类推,有时间逐一分享。
一、和风天气数据API
无广告,纯个人使用,每天有免费额度,1000次请求/天,个人使用足够了。
这里已本人使用的API为例,其余API相同道理,根据个人喜好来选择。
1、网址
2、创建项目
![](https://blog.ebancom.com/wp-content/uploads/2023/02/image-9-1024x496.png)
项目名和key名自己随便取,关键:记录key值
![](https://blog.ebancom.com/wp-content/uploads/2023/02/image-10-1024x496.png)
二、php配置
详情可查看和风天气开发文档,此处以昆明市西山区为例。
1、获取location的id
api请求地址:https://geoapi.qweather.com/v2/city/lookup?location=kunming&key=YOUR_KEY
由于地址加上adm参数,经常报错404 查询的数据或地区不存在。
故现在通过php代码来获取到昆明市西山区的locationid。
$url = "https://geoapi.qweather.com/v2/city/lookup?location=".$INFOconfig['region1']."&key=".$INFOconfig['key'];
$location = file_get_contents("compress.zlib://".$url,true);
$location = json_decode($location,true);
$location_num = count($location['location']);
// echo($location_num);
for ($i = 0; $i < $location_num; $i++) {
if ($INFOconfig['region2'] == $location['location'][$i]['name']) {
$location_id = $location['location'][$i]['id'];
echo("".$INFOconfig['region2']."的id为:".$location_id);
};
}
返回的结果如下:
![](https://blog.ebancom.com/wp-content/uploads/2023/02/image-11.png)
2、获取天气信息
api请求地址:https://api.qweather.com/v7/weather/3d?location=101190104&key=YOUR_KEY
$r_url = "https://devapi.qweather.com/v7/weather/3d?location=".$location_id."&key=".$INFOconfig['key'];
$weather = file_get_contents("compress.zlib://".$r_url,true);
$weather = json_decode($weather,true);
请求返回的数据为:
![](https://blog.ebancom.com/wp-content/uploads/2023/02/image-13-1024x241.png)
3、配置推送信息
我们需要推送的信息有:天气,气温,风向等。
![](https://blog.ebancom.com/wp-content/uploads/2023/02/image-14.png)
对应参数如上。
![](https://blog.ebancom.com/wp-content/uploads/2023/02/image-12-1024x401.png)
4、效果展示
![](https://blog.ebancom.com/wp-content/uploads/2023/02/微信图片_20230213101619-909x1024.png)
注:本篇内容在【微信公众测试号】定时推送消息给Ta基础之上有所改动。
若有不懂,请评论沟通交流。祝您顺利达成自己想要的结果。
1 条回复
[…] 【微信公众测试号】如何推送天气情况 […]