php 验证微信token_php之微信公众号验证token获取access_token

本文介绍了如何使用PhpStorm开发环境下实现微信公众号接口的Token验证与accessToken的缓存管理,包括checkToken函数的实现和getAccessToken方法的调用过程。

实例

/医院

* Created by PhpStorm.

* User: Administrator

* Date: 2018/6/1 0001

* Time: 上午 9:52

*/

namespace app\index\model;

use think\facade\Request;

use think\facade\Config;

use think\Model;

use think\Facade\Cache;

class WeixinModel extends Model

{

public function checkToken(){

$signature = Request::param('signature');

$timestamp = Request::param('timestamp');

$nonce = Request::param('nonce');

$echostr = Request::param('echostr');

$Token = Config::get('app.Token');

$tmpArr = array($timestamp, $nonce, $Token);

sort($tmpArr, SORT_STRING);

$tmpStr = implode($tmpArr);

$tmpStr = sha1($tmpStr);

if ($tmpStr != $signature) {

return false;

}

return $echostr;

}

public function getAccessToken($isCache = true)

{

if (!$isCache) {

Cache::rm("access_token");

}

$access_token = Cache::get("access_token");

if ($access_token && $isCache) {

return $access_token;

}

$APPID = Config::get('app.AppID');

$AppSecret = Config::get('app.AppSecret');

$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $APPID . "&secret=" . $AppSecret;

$res = http_Get($url);

$res = json_decode($res,true);

//        $access_token = Request::param('access_token');

//        $expires_in = Request::param('expires_in');

Cache::set("access_token", $res['access_token'], $res['expires_in'] - 600);

return $res['access_token'];

}

}

运行实例 »

点击 "运行实例" 按钮查看在线实例

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值