黄聪:如何高效率存储微信中的 access_token
众所周知,在微信开发中,获取
access_token的接口每天的调用次数是有限制的,2000次应该是。
不过其实这些完全够用了,除非你不小心写了个循环,在1秒中内用完了。
每个access_token的生效时间是2小时内,2小时过后就需要重新申请一下。其实每天只需要申请12次就可以了。
不管你怎么申请,当前申请了access_token,以前的就都不能用了。
所以解决办法就是:
在数据库中新建一个表token盛放申请来的access_token,字段有三个
access_token #存放access_token
expires #存放毫秒数
update_time #更新时间戳
- 1
- 2
- 3
逻辑是这样的:(表中只有一条数据)
查询表中数据
如果有数据,用 时间戳+更新时间 跟 现在时间比较,计算是否已过期:
如果已过期,重新申请,并更新数据库中数据,并返回access_token
如果没有过期,直接返回查询出的access_token
如果没数据:
申请数据并,插入数据库,返回access_tooken
- 1
- 2
- 3
- 4
- 5
- 6
/*
---------------------
获取access_token:
查询数据库中是否有数据,
如果有 取出数据
如果已经过期,查询,更新记录
如果没过期,直接返回数据
如果没有 查询并添加数据,返回数据
---------------------
*/ function get_token(){
$access_url= 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.AppID.'&secret='.AppScret;
//AppID AppScret 已经定义为常量 //数据库参数
$host= "1.1.1.1";
$port = '3306';
$database = "wx";
$user = "mysql";
$passwd = "mysql"; $db = new mysqli($host,$user,$passwd,$database,$port);
$sql = "select * from token"; //查询wx表数据
$exist = $db->query($sql); global $access_token; if($exist->num_rows)
//如果存在数据
{
$row = $exist->fetch_array();
//如果数据已过期
if($row['expires']+$row['update_time']<time()){
$token = json_decode(file_get_contents($access_url));
$sql = "update token set access_token = '{$token->access_token}' where access_token = '{$row['access_token']}'";
$db->query($sql);
$access_token = $token->access_token; //返回更新的token
} else {
$access_token = $row['access_token']; //返回查询的token
}
}
//如果没有数据
else {
$token = json_decode(file_get_contents($access_url));
$sql = "insert into token values('{$token->access_token}',{$token->expires_in},".time().")";
$db->query($sql);
$access_token = $token->access_token; //返回新建的token
}
$db->close(); return $access_token; //返回access_token
}
黄聪:如何高效率存储微信中的 access_token的更多相关文章
- 黄聪:日租VPS中FileZilla_Server配置方法
1.关闭VPS中IIS的FTP服务 2.FileZilla_Server 监听端口 21 3.FTP客户端端口为11311(看服务商给出的)
- 黄聪:wordpress在IIS8中设置默认编码(windows2012服务器)
web.config中配置 <?xml version="1.0" encoding="UTF-8"?> <configuration> ...
- 黄聪:WordPress制作插件中使用wp_enqueue_script('jquery')库不起作用解决方法
这个应该不是什么新信息,但我却是现在才搞清楚. 今天又是在wordpress调用jquery,情况还是如此.无意中打开wordpress中jquery.js,然后对比code.jquery.com中的 ...
- 黄聪:C#禁止Webbrowser中的脚本错误提示,自动屏蔽弹出窗口
using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; using ...
- 黄聪:微信公众平台开发OAuth2.0网页授权(转)
微信公众平台开发 OAuth2.0网页授权认证 网页授权获取用户基本信息 作者:方倍工作室 微信公众平台最近新推出微信认证,认证后可以获得高级接口权限,其中一个是OAuth2.0网页授权,很多朋友在使 ...
- 黄聪:VS2010开发如何在c#中使用Ctrl、Alt、Tab等全局组合快捷键
1.新建一个类 HotkeyHelper using System; using System.Runtime.InteropServices; using System.Windows.Forms ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (高级)
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (高级) Caching Application Bl ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(一) : Caching Application Block (初级)
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(一) : Caching Application Block (初级) 本篇文章具体官方解释请参照以下链接: h ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(九) Policy Injection Application Block
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(九) Policy Injection Application Block 代理对象(Proxy Object) ...
随机推荐
- 【转载】 PyTorch学习之六个学习率调整策略
原文地址: https://blog.csdn.net/shanglianlm/article/details/85143614 ----------------------------------- ...
- [LeetCode&Python] Problem 892. Surface Area of 3D Shapes
On a N * N grid, we place some 1 * 1 * 1 cubes. Each value v = grid[i][j] represents a tower of v cu ...
- xdoj-1324 (区间离散化-线段树求区间最值)
思想 : 1 优化:题意是覆盖点,将区间看成 (l,r)转化为( l-1,r) 覆盖区间 2 核心:dp[i] 覆盖从1到i区间的最小花费 dp[a[i].r]=min (dp[k])+a[i]s; ...
- ThinkPHP验证码类的使用
1.创建一个方法并引入验证码类class ShowAction extends Action{//用户评论验证码public function verify(){import('ORG.Util.Im ...
- LeetCode - Minimum Area Rectangle
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these p ...
- Full Schema Stitching with Apollo Server
转自: https://tomasalabes.me/blog/nodejs/graphql/apollo/2018/09/18/schema-stitiching-apollo.html Full ...
- loki grafana 团队开源的,类似Prometheus 的log 系统
Prometheus 主要面向的是metrics,但是loki 是log,这样加上grafana 强大的可视化以及alert能力, 我们可以做好多事情,loki 的设计来源于Prometheus. 组 ...
- Space Shooter 学习
using UnityEngine; using System.Collections; /// <summary> /// 背景滚动 /// </summary> publi ...
- python 简明教程 【转】
转自:https://learnxinyminutes.com/docs/python/ # Single line comments start with a number symbol. &quo ...
- jQuery 实现的瀑布流
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title> ...