示例#1
0
文件: Http.php 项目: Gwill/wechat
 /**
  * Make a request.
  *
  * @param string $url
  * @param string $method
  * @param array  $params
  * @param array  $options
  *
  * @return array|bool
  *
  * @throws FaultException
  * @throws HttpException
  */
 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 HttpException('Empty response.', -1);
     }
     // plain text or 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 FaultException("[{$contents['errcode']}] " . $contents['errcode'], $contents['errcode']);
     }
     if ($contents === array('errcode' => '0', 'errmsg' => 'ok')) {
         return true;
     }
     return $contents;
 }