微信公众号开发=》token存储

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;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容