美文网首页
数据库token缓存

数据库token缓存

作者: 沐盺zz | 来源:发表于2018-03-17 11:46 被阅读0次

    //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;
    }

    相关文章

      网友评论

          本文标题:数据库token缓存

          本文链接:https://www.haomeiwen.com/subject/ipzdqftx.html