存儲是以.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裏,參考文章↓
https://blog.csdn.net/sct_t/article/details/53002611