您尚未登录。

楼主 #1 2021-01-02 21:00:02

迷茫的微信开发加班狗
会员
注册时间: 2020-08-30
已发帖子: 2
积分: 2

PHP微信开发-access-token的获取与存储 (转)

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里,参考文章↓

https://blog.csdn.net/sct_t/article/details/53002611

离线

#2 2021-01-02 21:06:43

春风吹又生
会员
注册时间: 2020-02-25
已发帖子: 61
积分: 60

Re: PHP微信开发-access-token的获取与存储 (转)

用session保存不是更好么?

离线

页脚

工信部备案:粤ICP备20025096号 Powered by FluxBB

感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn