//token数据库缓存
function getAccessToken(){
//查询数据库是否有数据
//人为规定id1是token,id2是ticket
$res = getone("cachedata",$this->links,"id=1");
if($res){
//有存储token:判断时间是否过期
if($res['passtime'] > time()){
//token没有过期
$token = $res['datainfo'];
}else{
//token已经过期:重新获取token更新token
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";
$res1 = httpGet($url);
$res = json_decode($res1,true);
$token = $res['access_token'];
$passtime = time() + 7000;
$arr['datainfo'] = $token;
$arr['passtime'] = $passtime;
update($arr,$this->links,"cachedata","id=1");
}
}else{
//没有存储token:获取token添加到数据库
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";
$res1 = httpGet($url);
$res = json_decode($res1,true);
$token = $res['access_token'];
$passtime = time() + 7000;
//将数据添加到数据库
$arr['datainfo'] = $token;
$arr['passtime'] = $passtime;
$arr['id'] = 1;
$res = add($arr,$this->links,"cachedata");
// if($res){
// echo "添加成功";
// }else{
// echo "添加失败";
// }
}
return $token;
}
网友评论