zoukankan      html  css  js  c++  java
  • 使用缓存Memcache存储更新微信access token

    关键字:Memcache access_token 更新 存储 7200

    本文介绍如何使用缓存Memcache存储及更新 access token的方法。

    一、Access Token

    access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token。正常情况下access_token有效期为7200秒,重复获取将导致上次获取的access_token失效。

    公众号可以使用AppID和AppSecret调用本接口来获取access_token。AppID和AppSecret可在开发模式中获得(需要已经成为开发者,且帐号没有异常状态)。注意调用所有微信接口时均需使用https协议。

    接口调用请求说明

    http请求方式: GET
    https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
    

    参数说明

    参数是否必须说明
    grant_type 获取access_token填写client_credential
    appid 第三方用户唯一凭证
    secret 第三方用户唯一凭证密钥,既appsecret

    返回说明

    正常情况下,微信会返回下述JSON数据包给公众号:

    {"access_token":"ACCESS_TOKEN","expires_in":7200}

    二、Memcache

    Memcache是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨大的hash表,它能够用来存储各种格式的数据,包括图像、视频、文件以及数据库检索的结果等。简单的说就是将数据调用到内存中,然后从内存中读取,从而大大提高读取速度。
    Memcache是danga的一个项目,最早是LiveJournal 服务的,最初为了加速 LiveJournal 访问速度而开发的,后来被很多大型的网站采用。
    Memcached是以守护程序(监听)方式运行于一个或多个服务器中,随时会接收客户端的连接和操作。

    三、实现

    <?php
    
    define("APPID", "wx1111111111111111");
    define("APPSECRET", "11111111111111111111111111111111");
    
    $weixin = new class_weixin();
    var_dump($weixin);
    
    class class_weixin
    {
        var $appid = APPID;
        var $appsecret = APPSECRET;
    
        //构造函数,获取Access Token
        public function __construct($appid = NULL, $appsecret = NULL)
        {
            if($appid && $appsecret){
                $this->appid = $appid;
                $this->appsecret = $appsecret;
            }
            $mem = new Memcache;
            $mem->connect('localhost', 11211) or die ("Could not connect");
            $this->access_token = $mem->get('access_token');
            if (empty($this->access_token)){
                $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->appsecret;
                $res = $this->http_request($url);
                $result = json_decode($res, true);
                $this->access_token = $result["access_token"];
                $mem->set('access_token', $this->access_token, 0, 7200);
            }
        }
    
        //HTTP请求(支持HTTP/HTTPS,支持GET/POST)
        protected function http_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;
        }
    }
    ?>
    

      

    摘自  http://www.cnblogs.com/txw1958/p/weixin-access_token-memcache.html

  • 相关阅读:
    从string类的实现看C++类的四大函数 [写的很好]
    毕业5年决定你的命运
    git push 原因以及问题!
    poj 1195 Mobile phones 夜
    poj 2886 Who Gets the Most Candies 夜
    poj Asimple Problem With Integers 夜
    poj 2750 Potted Flower 夜
    poj 2528 Mayor's posters 夜
    poj 2777 Count Color 夜
    poj 2482 Stars in Your Window 夜
  • 原文地址:https://www.cnblogs.com/examine/p/4663398.html
Copyright © 2011-2022 走看看