Home > Database > Mysql Tutorial > body text

使用缓存Memcache存储更新微信access token

WBOY
Release: 2016-06-07 15:42:44
Original
1146 people have browsed it

关键字:Memcacheaccess_token 更新 存储 7200 本文介绍如何 使用 缓存 Memcache 存储 及 更新 access token的方法。 一、Access Token access_token是公众号的全局唯一票据,公众号调用各接口时都需 使用 access_token。正常情况下 access_token有效期为720

关键字: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
Copy after login

参数说明

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

返回说明

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

{"access_token":"ACCESS_TOKEN","expires_in":7200}
Copy after login

 

二、Memcache

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

 

三、实现

<span>php

</span><span>/*</span><span>
    方倍工作室 http://www.cnblogs.com/txw1958/
    CopyRight 2014 All Rights Reserved
</span><span>*/</span>

<span>define</span>("APPID", "wx1111111111111111"<span>);
</span><span>define</span>("APPSECRET", "11111111111111111111111111111111"<span>);

</span><span>$weixin</span> = <span>new</span><span> class_weixin();
</span><span>var_dump</span>(<span>$weixin</span><span>);

</span><span>class</span><span> class_weixin
{
    </span><span>var</span> <span>$appid</span> =<span> APPID;
    </span><span>var</span> <span>$appsecret</span> =<span> APPSECRET;

    </span><span>//</span><span>构造函数,获取Access Token</span>
    <span>public</span> <span>function</span> __construct(<span>$appid</span> = <span>NULL</span>, <span>$appsecret</span> = <span>NULL</span><span>)
    {
        </span><span>if</span>(<span>$appid</span> && <span>$appsecret</span><span>){
            </span><span>$this</span>->appid = <span>$appid</span><span>;
            </span><span>$this</span>->appsecret = <span>$appsecret</span><span>;
        }
        </span><span>$mem</span> = <span>new</span><span> Memcache;
        </span><span>$mem</span>->connect('localhost', 11211) or <span>die</span> ("Could not connect"<span>);
        </span><span>$this</span>->access_token = <span>$mem</span>->get('access_token'<span>);
        </span><span>if</span> (<span>empty</span>(<span>$this</span>-><span>access_token)){
            </span><span>$url</span> = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".<span>$this</span>->appid."&secret=".<span>$this</span>-><span>appsecret;
            </span><span>$res</span> = <span>$this</span>->http_request(<span>$url</span><span>);
            </span><span>$result</span> = json_decode(<span>$res</span>, <span>true</span><span>);
            </span><span>$this</span>->access_token = <span>$result</span>["access_token"<span>];
            </span><span>$mem</span>->set('access_token', <span>$this</span>->access_token, 0, 7200<span>);
        }
    }

    </span><span>//</span><span>HTTP请求(支持HTTP/HTTPS,支持GET/POST)</span>
    <span>protected</span> <span>function</span> http_request(<span>$url</span>, <span>$data</span> = <span>null</span><span>)
    {
        </span><span>$curl</span> =<span> curl_init();
        curl_setopt(</span><span>$curl</span>, CURLOPT_URL, <span>$url</span><span>);
        curl_setopt(</span><span>$curl</span>, CURLOPT_SSL_VERIFYPEER, <span>FALSE</span><span>);
        curl_setopt(</span><span>$curl</span>, CURLOPT_SSL_VERIFYHOST, <span>FALSE</span><span>);
        </span><span>if</span> (!<span>empty</span>(<span>$data</span><span>)){
            curl_setopt(</span><span>$curl</span>, CURLOPT_POST, 1<span>);
            curl_setopt(</span><span>$curl</span>, CURLOPT_POSTFIELDS, <span>$data</span><span>);
        }
        curl_setopt(</span><span>$curl</span>, CURLOPT_RETURNTRANSFER, 1<span>);
        </span><span>$output</span> = curl_exec(<span>$curl</span><span>);
        curl_close(</span><span>$curl</span><span>);
        </span><span>return</span> <span>$output</span><span>;
    }
}
</span>?>
Copy after login

 

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template