Beispiel #1
0
 /**
  * 获取access_token
  */
 public function getAccessToken()
 {
     static $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s';
     $token = Utility::getAccessToken($this);
     if (empty($token) || time() > $token['expired']) {
         $body = Utility::http(sprintf($url, $this->getConfig('appid'), $this->getConfig('appsecret')));
         $json = json_decode($body, true);
         if (!$json || !empty($json['errcode'])) {
             throw new Exception('Error - WeChat Can not get AccessToken.');
         } else {
             $token['token'] = $json['access_token'];
             $token['expired'] = time() + $json['expires_in'] - 120;
             Utility::setAccessToken($token, $this);
         }
     }
     return $token['token'];
 }