zoukankan      html  css  js  c++  java
  • 微信access_token请求之简单缓存方法封装

    还有东西要搞,就直接上代码

    function.php

    <?php 
    // 一个好玩的 curl 类
    // https://github.com/metowolf/Meting/blob/master/src/Meting.php  
    // curl 方法请求 url
    function https_request($url,$data)
    {
        // 初始化
        $ch = curl_init();
        // 设置
        curl_setopt($ch,CURLOPT_URL,$url);
        // 检查ssl证书
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
        // 从检查本地证书检查是否ssl加密
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,$url);
    
        if ( !empty($data) ) {
                curl_setopt($ch,CURLOPT_POST,1);// 开启post
                curl_setopt($ch,CURLOPT_POSTFIELDS,$data);// 发送post $data
    
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
        $res = curl_exec($ch);
        curl_close($ch); // close curl res
        return $res;
    }
    
    function __autoload($className)
    {
        if ( file_exists('./Controller/'.$className.'.php')) {
            include './Controller/'.$className.'.php';
        }
    }
    
    
    
     ?>

    Token.php

    <?php
    
    /**
    * token
    */
    class Token
    {
        public static $tokenFile = './token.txt';
        public static $tokenExpireTime = '3600';
    
        // 入口方法获取token
        public static function getToken()
        {
            // 先查缓存,  没有,再去请求
            if ( !self::checkTokenFileExists() || self::checkTokenFileExpire() ) {
                // 不存在 要重新请求
                // 过期 也要重新请求
                $token = self::request_token();
                // 更新缓存
                self::write_token($token);
                return $token;
            }else{
                // 缓存合法
                // 读取缓存
                return self::read_token();
    
            }
    
        }
    
        public static function write_token($token)
        {
            file_put_contents(self::$tokenFile,$token);
        }
        public static function read_token()
        {
            return file_get_contents(self::$tokenFile);
        }
    
    
        public static function request_token()
        {
            $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx199e07a40f2b94e7&secret=080b6aa95c137c95e1fb61c7eebfa0b2';
            // 发起 get
            $res = https_request($url);
            $data = json_decode($res,true);
            $token = $data['access_token'];
            // var_dump($token);
            return $token;
        }
    
        public static function checkTokenFileExists()
        {
            return file_exists(self::$tokenFile);
        }
        // 返回过期
        public static function checkTokenFileExpire()
        {
    
             return filemtime(self::$tokenFile) + self::$tokenExpireTime < time();
        }
    
    }
    
    
     ?>
  • 相关阅读:
    Python入门 日志打印
    Python入门 面向对象
    Python入门 更换pip源的方法
    Python入门 模块
    Python入门 函数式编程
    四月份该做的事情
    Docker入门 配置篇
    Python入门 序列章
    Python入门 值内存管理与所有的关键字
    论操作系统的IO
  • 原文地址:https://www.cnblogs.com/wangyang0210/p/9975804.html
Copyright © 2011-2022 走看看