微信公众号获取AccessToken,并保存到文件中,且过期前5分钟时再次自动获取

微信公众号获取AccessToken,并保存到文件中,且过期前5分钟时再次自动获取

韩昊杰
2022-04-07 / 2 评论 / 123 阅读 / 正在检测是否收录...
公用获取token,因微信公众号的 AccessToken 有效期是7200秒(两小时),且每日请求次数有限,所以我们可以将第一次请求的 AccessToken 保存到文件中,并且在过期前5分钟时再次自动获取,以减少 AccessToken 的实时请求次数浪费!

代码如下

//生成accesstoken
function getAccessToken()
{    
    // 微信公众平台appid及appsecret密钥
    $appid = 'wx232b4aa*********';
    $appsecret = '6b7f679961e76aa615e6588********';
 
    //储存access_token文件
    $logfilename = dirname(dirname(__FILE__)).'/online/accesstoken.txt';
 
    //如果不存在则创建一份文件
    if(!file_exists($logfilename)){
        file_put_contents($logfilename,'');
        //system("chmod 777 /data/wwwroot/blog/public/online/accesstoken.txt");
    }
 
    //读取内容
    $tokeninfo = file_get_contents($logfilename);
 
    //文件里面有内容
    if(!empty($tokeninfo)){
 
        //转换成数组形式
        $tokeninfo = json_decode($tokeninfo,true);
        if($tokeninfo['endtime']-time()<300){
            //预留5分钟的网络延迟时间,过期的话重新获取access_token
 
            //清空文档里面的内容
            file_put_contents($logfilename, '');
            $token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid.'&secret='.$appsecret;
 
            //获取文件内容或获取网络请求的内容
            $result = http_Msg($token_url,"GET");
            $fresult = $result;
 
            //access_token过期时间点
            $fresult["endtime"] = $result['expires_in']+time();
            $fresult = json_encode($fresult);
 
            //将access_token数据存在文件中
            file_put_contents($logfilename, $fresult);
        }else{
            //没有过期直接返回存储在文件中的access_token
            $result = $tokeninfo;
        }
    }else{
 
        //第一次获取access_token
        $token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid.'&secret='.$appsecret;
 
        //获取文件内容或获取网络请求的内容
        $result = http_Msg($token_url,"GET");
        $fresult = $result;
        $fresult["endtime"] = $result['expires_in']+time();
        $fresult = json_encode($fresult);
 
        //写入文件
        file_put_contents($logfilename, $fresult);
    }
    return $result;
}
 
// CURL模拟get/post请求
function http_Msg($url,$method="GET",$data=array())
{
    $ch = curl_init();
    $wx_data = json_encode($data);
 
    //打开
    $ch = curl_init();
 
    //请求方法为POST的时候
    if($method == "POST"){
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $wx_data);
    }
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    $response  = curl_exec($ch);
    //关闭
    curl_close($ch);
    $result = json_decode($response,true);
    return $result;
}

// 获取AccessToken
$AccessToken = getAccessToken();

1

评论 (2)

取消
  1. 头像
    tony
    iPhone · Google Chrome

    存redis 或者服务器缓存

    回复
    1. 头像
      韩昊杰 作者
      Windows 10 · Google Chrome
      @ tony

      之前的文章了 现在都存redis了

      回复