access_token的获取,以及在ThinkPHP中的F方法全局保存

来源:互联网 发布:电脑程序员培训班 编辑:程序博客网 时间:2024/06/05 16:35


access_token的获取,以及在ThinkPHP中的F方法全局保存


access_token的获取不能在程序的多个地方的获取保存,以免造成失效的BUG


 // access_token 应该全局存储与更新,以下代码以写入到文件中做示例    $data = json_decode(F('access_token'));    if ($data->expire_time < time()) {      // 如果是企业号用以下URL获取access_token      // $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret";      $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;        F('access_token', json_encode($data));      }    } else {      $access_token = $data->access_token;    }

采用了F函数


原创粉丝点击