Пример #1
0
 /**
  * 发起一个HTTP/HTTPS的请求
  *
  * @param string $url     接口的URL
  * @param string $method  请求类型   GET | POST
  * @param array  $params  接口参数
  * @param array  $options 其它选项
  *
  * @return array | boolean
  */
 public function request($url, $method = self::GET, $params = array(), $options = array())
 {
     if ($this->token) {
         $url .= (stripos($url, '?') ? '&' : '?') . 'access_token=' . $this->token;
     }
     $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('服务器无响应');
     }
     // 文本或者json
     $textMIME = '~application/json|text/plain~i';
     $contents = JSON::decode($response['data'], true);
     // while the response is an invalid JSON structure, returned the source data
     if (!preg_match($textMIME, $response['content_type']) || JSON_ERROR_NONE !== json_last_error() && false === $contents) {
         return $response['data'];
     }
     if (isset($contents['errcode']) && 0 !== $contents['errcode']) {
         if (empty($contents['errmsg'])) {
             $contents['errmsg'] = 'Unknown';
         }
         throw new Exception("[{$contents['errcode']}] " . $contents['errmsg'], $contents['errcode']);
     }
     if ($contents === array('errcode' => '0', 'errmsg' => 'ok')) {
         return true;
     }
     return $contents;
 }
Пример #2
0
 /**
  * 发起一个HTTP/HTTPS的请求
  *
  * @param string $url     接口的URL
  * @param string $method  请求类型   GET | POST
  * @param array  $params  接口参数
  * @param array  $options 其它选项
  *
  * @return array | boolean
  */
 public function request($url, $method = self::GET, $params = array(), $options = array())
 {
     if ($this->token) {
         $url .= (stripos($url, '?') ? '&' : '?') . 'access_token=' . $this->token;
     }
     $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(substr(str_replace(['\\"', '\\\\'], ['"', ''], json_encode($response['data'])), 1, -1), 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';
         }
         throw new Exception("[{$contents['errcode']}] " . $contents['errmsg'], $contents['errcode']);
     }
     if ($contents === array('errcode' => '0', 'errmsg' => 'ok')) {
         return true;
     }
     return $contents;
 }
Пример #3
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();
     }
     $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;
 }