Example #1
0
 /**
  * 获取access_token 字符串
  * @return string
  */
 public function access_token()
 {
     $token_file = FileStatic::mkDir(APPPATH . 'cache/wechat') . $this->appid . '.json';
     $get = FALSE;
     // 是否需要再次远程获取
     if (!file_exists($token_file)) {
         $get = TRUE;
     } else {
         $json = json_decode(file_get_contents($token_file));
         if ($json->expires < time()) {
             $get = TRUE;
         } else {
             return $json->access_token;
         }
     }
     if ($get) {
         $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s';
         $json = json_decode($this->curlGet(sprintf($url, $this->appid, $this->appSecret)));
         if (!empty($json->errcode)) {
             // 获取失败直接停止
             die('get access_token fail!');
         }
         $data = array('expires' => time() + $json->expires_in, 'access_token' => $json->access_token);
         file_put_contents($token_file, json_encode($data));
         return $json->access_token;
     }
     return '';
 }
Example #2
0
 protected function accessToken()
 {
     $token_file = FileStatic::mkDir(APPPATH . 'cache/wechat') . $this->CorpID . '.json';
     if (file_exists($token_file) && !empty($json = json_decode(file_get_contents($token_file))) && $json->expires > time()) {
         return $json->access_token;
     }
     $url = sprintf('https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s', $this->CorpID, $this->Secret);
     $json = json_decode($this->curlGet($url));
     if (!empty($json->errcode)) {
         // 获取失败直接停止
         die('get access_token fail!');
     }
     $data = ['expires' => time() + $json->expires_in, 'access_token' => $json->access_token];
     file_put_contents($token_file, json_encode($data));
     return $json->access_token;
 }