public function getWxAccessToken(){
$tokenFile = "./access_token.txt"; // 缓存文件名
$data = json_decode(file_get_contents($tokenFile)); //转换为json格式
if ($data->expire_time < time() or ! $data->expire_time) {
//token过期的情况
$AppId = '';
$AppSecret = '';
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$AppId}&secret={$AppSecret}";
$res = $this->http_curl($url);
$access_token = $res['access_token'];
if ($access_token) {
$data['expire_time'] = time() + 3600; //保存1小时
$data['access_token'] = $access_token;
$fp = fopen($tokenFile, "w"); //只写文件
fwrite($fp, json_encode($data)); //写入json格式文件
fclose($fp); //关闭连接
}
} else {
$access_token = $data->access_token;
}
return $access_token;
}
网友评论