Skip to content

Commit

Permalink
增加缓存ACCESS_TOKEN
Browse files Browse the repository at this point in the history
  • Loading branch information
KaiXin committed Dec 7, 2021
1 parent a2e8816 commit 87db6b3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
6 changes: 3 additions & 3 deletions readme/weixin.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
1.先在上面的地址中申请一个接口测试号;
2.weixin.php第61行中填写appID和appsecret;
2.weixin.php第75行中填写appID和appsecret;
3.把msg.php上传到自己的空间或者中,当然用我的也可以(http://域名/msg.php?title=标题(可选值)&time=提交时间&msg=推送内容);
4.修改weixin.php第67行,touser是具体推送的用户,也就是微信号(不是普通的微信号,在面板中查看)
4.修改weixin.php第85行,touser是具体推送的用户,也就是微信号(不是普通的微信号,在面板中查看)
![微信号](https://github.com/kaixin1995/InformationPush/blob/master/image/%E5%BE%AE%E4%BF%A1%E5%8F%B7%E6%9F%A5%E7%9C%8B.png)
5.后台创建模板,格式可以按照我的,如果修改,请同步修改weixin.php中的代码;
![创建模板](https://github.com/kaixin1995/InformationPush/blob/master/image/%E6%96%B0%E5%A2%9E%E6%A8%A1%E6%9D%BF.png)
6.修改weixin.php第70行,这个值是后台中的模板值;
6.修改weixin.php第88行,这个值是后台中的模板值;

## 推送成功
![推送成功](https://github.com/kaixin1995/InformationPush/blob/master/image/%E6%8E%A8%E9%80%81%E6%88%90%E5%8A%9F.png)
Expand Down
22 changes: 20 additions & 2 deletions weixin.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php
date_default_timezone_set('PRC');
ini_set('session.gc_maxlifetime', "18000"); // 5小时
ini_set("session.cookie_lifetime","18000"); // 5小时

//如果不存在文本就禁止提交
if(!isset($_REQUEST['msg']))
Expand Down Expand Up @@ -57,8 +59,24 @@ function https_request($url, $data = null)
* 开始推送
*/

//替换你的ACCESS_TOKEN
$ACCESS_TOKEN = json_decode(https_request("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=替换自己的APPID&secret=替换自己的APPSECRET"),true)["access_token"];
if(!session_id()){
session_start();
}
$ACCESS_TOKEN ='';
if (isset($_SESSION['WXACCESS_TOKEN']))
{
//存在
$ACCESS_TOKEN =$_SESSION['WXACCESS_TOKEN'];
echo '从缓存中获取的ACCESS_TOKEN';
}
else
{
//替换你的ACCESS_TOKEN
$ACCESS_TOKEN = json_decode(https_request("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=替换自己的APPID&secret=替换自己的APPSECRET"),true)["access_token"];
$_SESSION['WXACCESS_TOKEN']=$ACCESS_TOKEN;
echo '重获取的ACCESS_TOKEN';
}

//模板消息请求URL
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $ACCESS_TOKEN;
$MsgArray=array();
Expand Down
24 changes: 22 additions & 2 deletions work.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php
date_default_timezone_set('PRC');
ini_set('session.gc_maxlifetime', "18000"); // 5小时
ini_set("session.cookie_lifetime","18000"); // 5小时

//如果不存在文本就禁止提交
if(!isset($_REQUEST['msg']))
Expand Down Expand Up @@ -53,8 +55,26 @@ function https_request($url, $data = null)
* 开始推送
*/

//替换你的ACCESS_TOKEN
$ACCESS_TOKEN = json_decode(https_request("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=企业ID&corpsecret=应用的Secret"),true)["access_token"];
if(!session_id()){
session_start();
}

$ACCESS_TOKEN ='';

if (isset($_SESSION['WorkACCESS_TOKEN']))
{
//存在
$ACCESS_TOKEN =$_SESSION['WorkACCESS_TOKEN'];
echo '从缓存中获取的ACCESS_TOKEN';
}
else
{
//替换你的ACCESS_TOKEN
$ACCESS_TOKEN = json_decode(https_request("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=企业ID&corpsecret=应用的Secret"),true)["access_token"];
$_SESSION['WorkACCESS_TOKEN']=$ACCESS_TOKEN;
echo '重获取的ACCESS_TOKEN';
}

//模板消息请求URL
$url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=".$ACCESS_TOKEN;
$MsgArray=array();
Expand Down

0 comments on commit 87db6b3

Please sign in to comment.