https://blog.csdn.net/HOLYLANCES/article/details/82018065
存储是以.txt的方式存储token值的 先上代码
<?php
require_once("file_cache.php");
class WeixinUtil{
private $appId = 'appId ';
private $appSecret = 'appSecret ';
//获取AccessToken
public function getAccessToken(){
$wx_access_token_cache_key = 'wx_access_token';
$cache = new FileCache('./access_token.txt');//存储的本地位置
$token = $cache->get($wx_access_token_cache_key);//去文件中获取token
if (!$token){//没有的话就创建新的
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appId."&secret=".$this->appSecret;
$token = $this->https_curl_json($url,null);
$cache->set($wx_access_token_cache_key, $token, time()+7000);
}
$token = json_decode($token);
$token = $token->access_token;
return $token;
}
/* 发送json格式的数据,到api接口*/
public function https_curl_json($url,$data){
$headers = array("Content-type: application/json;charset=UTF-8","Accept: application/json","Cache-Control: no-cache", "Pragma: no-cache");
$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);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers );
$output = curl_exec($curl);
if (curl_errno($curl)) {
echo 'Errno'.curl_error($curl);//捕抓异常
}
curl_close($curl);
return $output;
}
}
?>
另一个类 用来进行文件操作
<?php
//该类用来配合WeixinUtil.php类 进行token的文件操作
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
include "WeixinUtil.php";
$wx = new WeixinUtil();
echo $wx->getAccessToken();
第二个类来自https://www.cnblogs.com/hydonlee/p/5419063.html
=======================================================================================
18/9/11
access_token存储在DB里,参考文章↓
离线
用session保存不是更好么?
离线
用session保存不是更好么?
session 不能分布式布局的。token可以方便改为分布式。
离线