网上找了很多教程也没有PHP版本的 就自己写了一个 写了好久了一直没时间分享出来
过程也很简单 几分钟就可以配置好
我们现在开始
注册微信公众测试号
- 点开下面的链接打开微信公众测试号注册页面
https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login
- 打开并注册一个微信公众号测试号,扫码登录即可。
- 扫码登录成功后,就会生成微信公众号测试号的appID和appsecret信息,要记住,后面会用!!!
扫描测试号二维码
- 向下滑动,找到测试号二维码,使用微信扫描测试号二维码并关注。
- 用户列表会自动显示用户信息,生成一个特殊的微信号。
- 这里需要记住的是对应的微信号,后续会用到。
- 是需要接收推送消息的那个人扫码。要是给其他人做,自己可以扫一下,测试好后,再让对方扫码。
新增测试模板
- 点击 新增测试模板
模板如下:
{{date.DATA}} 地区:{{region.DATA}}
今日天气:{{todayweather.DATA}}
明日天气:{{nextdayweather.DATA}}
最高气温:{{maxc.DATA}} 最低气温:{{minc.DATA}}
气温:{{temp.DATA}}
风向:{{wind_dir.DATA}}
湿度:{{humidity.DATA}}
今天是我们恋爱的第{{love_day.DATA}}天 {{content.DATA}}
配置代码参数
根据注释填入相应的信息就好了
运行
只需要放在有PHP环境的网站根目录下访问(你的网站/wx.php)即可
定时推送
有宝塔的可以使用宝塔的计划任务功能
代码
<?php
header('Content-Type: application/json; charset=UTF-8');
function randColor(){
$colors=array('5CB85C','428BCA','FF6600','D9534F','B37333','00ABA9');
$show_color = array_rand($colors, 1);
return "#".$colors[$show_color];
}
function get_curl($url, $post = 0, $referer = 0, $cookie = 0, $header = 0, $ua = 0, $nobaody = 0)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$klsf[] = "Accept:*/*";
$klsf[] = "content-type:application/json;charset=UTF-8";
curl_setopt($ch, CURLOPT_HTTPHEADER, $klsf);
if ($post) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}
if ($header) {
curl_setopt($ch, CURLOPT_HEADER, TRUE);
}
if ($cookie) {
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
}
if ($referer) {
if ($referer == 1) {
curl_setopt($ch, CURLOPT_REFERER, "http://m.qzone.com/infocenter?g_f=");
} else {
curl_setopt($ch, CURLOPT_REFERER, $referer);
}
}
if ($ua) {
curl_setopt($ch, CURLOPT_USERAGENT, $ua);
} else {
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Linux; U; Android 4.0.4; es-mx; HTC_One_X Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0');
}
if ($nobaody) {
curl_setopt($ch, CURLOPT_NOBODY, 1);
//主要头部
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);//跟随重定向
}
curl_setopt($ch, CURLOPT_ENCODING, "gzip");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$ret = curl_exec($ch);
curl_close($ch);
return $ret;
}
function tianqiapi($city)
{
$city = urlencode($city);
$info = get_curl("https://v0.yiketianqi.com/api?unescape=1&version=v91&appid=43656176&appsecret=I42og6Lm&ext=&cityid=&city=".$city);
$info = json_decode($info,true);
return $info;
}
function caihongpi($key)
{
$result_arr = get_curl('http://api.tianapi.com/caihongpi/index?key='.$key);
$result = json_decode($result_arr,true);
return $result['newslist'][0]['content'];
}
$tianqi_info = tianqiapi("北京"); //填写城市
$appID = ''; //填写APPID
$appsecret = ''; //填写APPSECRET
$key = ""; //填写天行数据彩虹屁的key
$love_time = "2021-11-06"; //填写你们在一起的时间 年份-月份-日 ps:日期为123456岗位上前要加0
$touser = ''; //填写发送用户的OPENID
$template_id = ""; //填写模板ID
$access_token_array = get_curl('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appID.'&secret='.$appsecret);
$access_token = json_decode($access_token_array,true)['access_token'];
$data = [
"touser" => $touser,
"template_id" => $template_id,
"url" => "http://weixin.qq.com/download",
"topcolor" => "#FF0000",
"data" => [
"date" => [
"value" => $tianqi_info['data'][0]['date']." ".$tianqi_info['data'][0]['week'],
"color" => randColor()
],
"region" => [
"value" => $tianqi_info['city'],
"color" => randColor()
],
"todayweather" => [
"value" => $tianqi_info['data'][0]['wea'],
"color" => randColor()
],
"nextdayweather" => [
"value" => $tianqi_info['data'][1]['wea'],
"color" => randColor()
],
"maxc" => [
"value" => $tianqi_info['data'][0]['tem1'],
"color" => randColor()
],
"minc" => [
"value" => $tianqi_info['data'][0]['tem2'],
"color" => randColor()
],
"temp" => [
"value" => $tianqi_info['data'][0]['tem'],
"color" => randColor()
],
"wind_dir" => [
"value" => $tianqi_info['data'][0]['win'][0].",".$tianqi_info['data'][0]['win'][1].",".$tianqi_info['data'][0]['win_speed'],
"color" => randColor()
],
"humidity" => [
"value" => $tianqi_info['data'][0]['humidity'],
"color" => randColor()
],
"love_day" => [
"value" => (strtotime(date("Y-m-d")) - strtotime(date($love_time)))/86400,
"color" => randColor()
],
"content" => [
"value" => caihongpi($key),
"color" => randColor()
],
]
];
$info = get_curl('https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$access_token,json_encode($data));
$info = json_decode($info,true);
if ($info['errcode'] == 0){
$result = ['code' => 200,'msg' => "推送成功"];
}else{
$result = get_curl('https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$access_token,json_encode($data));
}
exit(json_encode($result));
?>