給女友的微信公眾號消息推送 跟上次的大致相同、新增了每日新聞、國外天氣,修改了一部分代碼 一、註冊易客雲 地址:http://www.yiketianqi.com/user/register 複製下麵的代碼到百度雲函數代碼編輯頁 <?php $appId = '2323232'; //對應自己的ap ...
跟上次的大致相同、新增了每日新聞、國外天氣,修改了一部分代碼
一、註冊易客雲
地址:http://www.yiketianqi.com/user/register
複製下麵的代碼到百度雲函數代碼編輯頁
<?php
$appId = '2323232'; //對應自己的appId
$appSecret = '343444453'; //對應自己的appSecret
$wxgzhurl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appId . "&secret=" . $appSecret;
$access_token_Arr = https_request($wxgzhurl);
$access_token = json_decode($access_token_Arr, true);
$ACCESS_TOKEN = $access_token['access_token']; //ACCESS_TOKEN
// 什麼時候戀愛的(格式別錯)
$lovestart = strtotime('2012-12-22');
$end = time();
$love = ceil(($end - $lovestart) / 86400);
// 下一個生日是哪一天(格式別錯)
$birthdaystart = strtotime('2023-12-02');
$end = time();
$diff_days = ($birthdaystart - $end);
$birthday = (int)($diff_days/86400);
$birthday = str_replace("-", "", $birthday);
$tianqiurl = 'https://yiketianqi.com/api?version=v5&appid=12345678&appsecret=12345678&city=堪培拉'; //修改為自己的測試
$tianqiapi = https_request($tianqiurl);
$tianqi = json_decode($tianqiapi, true);
$qinghuaqiurl = 'https://api.youguo56.com/api/chp/';
$qinghuaapi = https_request($qinghuaqiurl);
#$qinghua = json_decode($qinghuaapi, true);
$yjh = '';
$touser = 'vfvfvfv'; //這個填你女朋友的openid
$data = array(
'touser' => $touser,
'template_id' => "regrgagff", //改成自己的模板id,在微信後臺模板消息里查看
'url'=>"https://api.youguo56.com/api/60s/",
'data' => array(
'first' => array(
'value' => $yjh,
'color' => "#00BFFF"
),
'keyword1' => array(
'value' => $tianqi['update_time'],//時間
'color' => "#1E90FF"
),
'keyword2' => array(
'value' => $tianqi['country'],//國家
'color' => "#7B68EE"
),
'keyword3' => array(
'value' => $tianqi['city'],//城市
'color' => "#7B68EE"
),
'keyword4' => array(
'value' => $tianqi['data']['0']['wea'],//天氣
'color' => "#FFA07A"
),
'keyword5' => array(
'value' => $tianqi['data']['0']['tem1'],//高溫
'color' => "#98FB98"
),
'keyword6' => array(
'value' => $tianqi['data']['0']['tem2'],//低溫
'color' => "#98FB98"
),
'keyword8' => array(
'value' => $love . '天',
'color' => "#FA8072"
),
'keyword9' => array(
'value' => $birthday . '天',
'color' => "#F5DEB3"
),
'remark' => array(
'value' => $qinghuaapi,
'color' => "#6495ED"
),
)
);
$json_data = json_encode($data);
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $ACCESS_TOKEN;
$res = https_request($url, urldecode($json_data));
$res = json_decode($res, true);
if ($res['errcode'] == 0 && $res['errcode'] == "ok") {
echo "發送成功!<br/>";
}else {
echo "發送失敗!請檢查代碼!!!<br/>";
}
function https_request($url, $data = null)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)) {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
function handler($context) {
# 啥都沒有 就是為了填執行失敗的坑
# return $ret;
}
複製自己的appid和 appsecret 換成自己的appid、appsecret、城市
新模板內容
現在是{{keyword1.DATA}}
國家:{{keyword2.DATA}}
城市:{{keyword3.DATA}}
天氣:{{keyword4.DATA}}
最高溫度:{{keyword5.DATA}} ℃
最低溫度:{{keyword6.DATA}} ℃
今天是我們在一起的第{{keyword8.DATA}}
距離寶貝的生日還有{{keyword9.DATA}}
每日一句: {{remark.DATA}}
在這裡新增了每日新聞,如果不想要可以刪掉這代碼。
測試成功
期待下次的分享,別忘了三連支持博主呀~
我是 念舒_C.ying ,期待你的關註~