Blogger Information
Blog 53
fans 4
comment 3
visits 41707
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php之微信公众号验证token获取access_token
有点凉了
Original
1407 people have browsed it

123.gif

实例

<?php
/医院
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/1 0001
 * Time: 上午 9:52
 */

namespace app\index\controller;


use think\Controller;
use think\facade\Config;
use think\facade\Cache;
use think\facade\Request;

//注意驼峰规则命名的控制器在url中访问中间是要加 _ 下划线的 略坑 不知道为什么这么设计
class Weixin extends Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->model = model('WeixinModel');//这块控制器名字写错了 难怪出不来。。。。
    }
    //验证签名
    public function check()
    {
        $valid = $this->model->checkToken();
        if (!$valid) {
            exit('signature error');
        }
        exit(input('get.echostr'));
    }
    //获取access_token
    public function getAccessToken(){
         return $this->model->getAccessToken(true);
    }

}

运行实例 »

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

实例

<?php
/医院
 * 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'];
    }
}

运行实例 »

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


Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post