微信 getAccessToken方法详解及实例
memcache缓存存储用户信息7000秒
<?php
functiongetAccessToken($appid,$appsecret)
{
$mem=newCacheMemcache();
$acc=$mem->get('access_token_'.$appid);
if(!$acc)
{
$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
$result=https_request($url);
$jsoninfo=json_decode($result,true);
$access_token=$jsoninfo['access_token'];
if($access_token)
{
$expire=time()+7000;
$mem=newCacheMemcache();
$mem->set('access_token_'.$appid,$access_token,$expire);
}
}
else
{
$access_token=$acc;
}
return$access_token;
}
?>
文件存储access_token
functiongetAccessToken(){
//access_token应该全局存储与更新,以下代码以写入到文件中做示例
$data=json_decode(file_get_contents("access_token.json"));
if($data->expire_time<time()){
$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";
$res=json_decode($this->httpGet($url));
$access_token=$res->access_token;
if($access_token){
$data->expire_time=time()+7000;
$data->access_token=$access_token;
$fp=fopen("access_token.json","w");
fwrite($fp,json_encode($data));
fclose($fp);
}
}else{
$access_token=$data->access_token;
}
return$access_token;
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下