微信公众号开发=》token存储
2017-03-14 本文已影响0人
超人又来了
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;
}