语言:PHP

access_token一直要用,但每天取的数量有限制。反正2小时才过期。就想缓存一下。

File1: wx_access_token.php

File2: file_cache.php

---------------------------

File1: wx_access_token.php

<?php
/*
* wx_access_token.php
*
* get the weixin access token
* */
if (!defined("DOCUMENT_ROOT")) define("DOCUMENT_ROOT", $_SERVER['DOCUMENT_ROOT']);
if (!defined("__HOME__")) define("__HOME__", dirname(DOCUMENT_ROOT)); require_once("file_cache.php"); class WxAccessToken{
public function getToken(){
$wx_access_token_cache_key = 'wx_access_token'; $cache = new FileCache(__HOME__ . '/myfolder/cache_file.txt');
$token = $cache->get($wx_access_token_cache_key); if (!$token){
$token = $this->getTokenFromWx();
$cache->set($wx_access_token_cache_key, $token, time()+7000);
} return $token;
} private function getTokenFromWx(){
$appid = "your appid";
$appsecret = "your app secret";
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch); $jsoninfo = json_decode($output, true); return $jsoninfo["access_token"];
}
}
?>

File2: file_cache.php

<?php
/*
* file_cache.php
*
* cache objects via json with filesystem
* author: hydon lee
* */ /*
class FileCache examples:
$cache = new FileCache('../myfolder/cache_file.txt');
$cache->set('username', 'lihd', time()+3600);
$username = $cache->get('username');
echo $username;
*/
class FileCache{
private $cache_file; private function load(){
if(file_exists($this->cache_file)){
$content = file_get_contents($this->cache_file); if (strlen($content) > 0){
$data = json_decode($content); return $data;
}
} return array();
} private function save($data){
$content = json_encode($data);
return file_put_contents($this->cache_file, $content);
} public function __construct($filename) {
$this->cache_file = $filename;
} public function get($key){
$data = $this->load(); foreach($data as $item){
if ($item->key == $key){
if ($item->expire_time > time()){
return $item->value;
} break;
}
} return NULL;
} public function set($key, $value, $expire_time=NULL){
$data = $this->load(); $obj = NULL;
foreach($data as $item){
if ($item->key == $key){
$obj = $item; $obj->value = $value;
if ($expire_time != NULL){
$obj->expire_time = $expire_time;
} break;
}
} if ($obj == NULL){
$obj = new CacheItem($key, $value, $expire_time);
array_push($data, $obj);
} return $this->save($data);
}
} class CacheItem{
public $key;
public $value;
public $expire_time; public function __construct($key, $value, $expire_time) {
$this->key = $key;
$this->value = $value;
$this->expire_time = $expire_time;
}
}
?>

php微信开发(1):缓存access_token的方法的更多相关文章

  1. 【微信开发】缓存的asscess_token过期

    开发中有遇到这样一个问题,我们一般会将从微信拿到的寿命2个小时的access_token缓存起来,业务里这个缓存的时间是90分钟, 90分钟之后缓存过期,会重新请求新的access_token使旧的a ...

  2. 夺命雷公狗---微信开发13----获取access_token

    获得Access Token的方法1: 这里可以手动进行修改: https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential ...

  3. 微信开发所需要的的方法(签名认证、数组转字符串方法、将xml字符串转换为数组、发送xml请求方法)

    //将xml字符串转换为数组 public function xmlToArray($xml){ $array_data = json_decode(json_encode(simplexml_loa ...

  4. 微信开发笔记:公众号获取access_token

    微信开发中,access_token的获取是一种非常常见的功能,通过公众号的appid和appsecret来向微信公众平台请求一个临时通行凭证:access_token.公众平台上的绝大部分操作都会需 ...

  5. 第五篇 :微信公众平台开发实战Java版之如何获取公众号的access_token以及缓存access_token

    一.access_token简介 为了使第三方开发者能够为用户提供更多更有价值的个性化服务,微信公众平台 开放了许多接口,包括自定义菜单接口.客服接口.获取用户信息接口.用户分组接口.群发接口等, 开 ...

  6. Java开发微信公众号(五)---微信开发中如何获取access_token以及缓存access_token

    获取access_token是微信api最重要的一个部分,因为调用其他api很多都需要用到access_token.比如自定义菜单接口.客服接口.获取用户信息接口.用户分组接口.群发接口等在请求的时候 ...

  7. PHP定时任务获取微信access_token的方法

    一.使用brew安装php多版本方法 # brew install php56# brew install php70二.安装切换工具 # brew install php-version# sour ...

  8. [麦先生]TP3.2之微信开发那点事[基础篇](获取access_token)

    在微信文档中一共提供了两个access_token:一个是伪全局配置的Access_token;一个是在微信网页授权时的小Access_token 很多刚刚接触微信开发的人经常会混淆这两个的作用: 我 ...

  9. php - 微信 - 缓存access_token类。

    可扩展性很强. <?php namespace LaneWeChat\Core; /** * 微信Access_Token的获取与过期检查 * Created by Lane. * User: ...

随机推荐

  1. jstree 插件的使用笔记(一)

    官方:http://www.jstree.com/  一.节点的描述 官方资料:http://www.jstree.com/docs/json/ 格式一 { id : "string&quo ...

  2. iOS 下拉刷新 上拉加载实现原理

    1.下拉刷新 实现原理 if (scrollView.contentOffset.y < -100) { [UIView animateWithDuration:1.0 animations:^ ...

  3. nginx设置SSL反向代理

    Nginx的反向代理通常用来映射内网中提供的Apache.IIS.Lighttpd服务,以实现负载均衡:同时,由于动态服务程序运行在内网,服务器的整体安全性也有所提高,那么怎样用nginx设置SSL的 ...

  4. pxe+kickstart实现无人值守批量安装linux

    pxe+kickstart实现无人值守批量安装linux 实验准备:主机myrhel2作为服务器端,新建一个没有安装操作系统的虚拟主机,而且其与服务器端在同一个网段 安装的条件: 服务器端:      ...

  5. 转: 在线office的协作工具列表

    产品完备性.协作功能.功能完备性对比 &lt;img src="https://pic2.zhimg.com/af14a86eedcb93962e41e47788de5989_b.p ...

  6. css 雪碧图的制作

    很多网站其实都用了雪碧图,确实方便了制作,以前以为这种小图标,都是一个一个图片呢(笑) 效果图如下: 代码如下: <html> <head lang="en"&g ...

  7. MongoDB - The mongo Shell, Configure the mongo Shell

    Customize the Prompt You may modify the content of the prompt by setting the variable prompt in the  ...

  8. photoSlider-原生js移动开发轮播图、相册滑动插件

    详细内容请点击 在线预览   立即下载 使用方法: 分别引用css文件和js文件 如: <link rel="stylesheet" type="text/css& ...

  9. Miniui updateRow更改列字段值

    当UPC等于upccode时,更改列Scanned+1 //先grid.findRow找到UPC等于upccode的行对象 var row = grid.findRow(function (row) ...

  10. 图解SQL的inner join(join)、left join、right join、full outer join、union、union all的区别

    对于SQL的Join,在学习起来可能是比较乱的.我们知道,SQL的Join语法有很多inner的,有outer的,有left的,有时候,对于Select出来的结果集是什么样子有点不是很清楚.Codin ...