Beispiel #1
0
 /**
  * 发起一个HTTP/HTTPS的请求
  *
  * @param string $url     接口的URL
  * @param string $method  请求类型   GET | POST
  * @param array  $params  接口参数
  * @param array  $options 其它选项
  * @param int    $retry   重试次数
  *
  * @return array | boolean
  */
 public function request($url, $method = self::GET, $params = array(), $options = array(), $retry = 1)
 {
     if ($this->token) {
         $url .= (stripos($url, '?') ? '&' : '?') . 'access_token=' . $this->token->getToken();
     }
     if ($this->agentId) {
         $url .= (stripos($url, '?') ? '&' : '?') . 'agentid=' . $this->agentId;
     }
     $method = strtoupper($method);
     if ($this->json) {
         $options['json'] = true;
     }
     $response = parent::request($url, $method, $params, $options);
     $this->json = false;
     if (empty($response['data'])) {
         throw new Exception('服务器无响应');
     }
     if (!preg_match('/^[\\[\\{]\\"/', $response['data'])) {
         return $response['data'];
     }
     $contents = json_decode($response['data'], true);
     // while the response is an invalid JSON structure, returned the source data
     if (JSON_ERROR_NONE !== json_last_error()) {
         return $response['data'];
     }
     if (isset($contents['errcode']) && 0 !== $contents['errcode']) {
         if (empty($contents['errmsg'])) {
             $contents['errmsg'] = 'Unknown';
         }
         // access token 超时重试处理
         if ($this->token && in_array($contents['errcode'], array('40001', '42001')) && $retry > 0) {
             // force refresh
             $this->token->getToken(true);
             return $this->request($url, $method, $params, $options, --$retry);
         }
         throw new Exception("[{$contents['errcode']}] " . $contents['errmsg'], $contents['errcode']);
     }
     if ($contents === array('errcode' => '0', 'errmsg' => 'ok')) {
         return true;
     }
     return $contents;
 }
 public function __construct(Config $config)
 {
     parent::__construct($config->appId, $config->appSecret);
 }
Beispiel #3
0
 /**
  * constructor
  *
  * @param AccessToken $token
  */
 public function __construct($token = null)
 {
     $this->token = $token instanceof AccessToken ? $token->getToken() : $token;
     parent::__construct();
 }
 public function updateToken()
 {
     $accessToken = new AccessToken($this->_appId, $this->_secret);
     $token = $accessToken->getToken();
     $cache = new Cache($this->_appId);
     $this->show($token);
     $cache->forget('overtrue.wechat.access_token');
     $this->show($accessToken->getToken());
 }