/** * _request * 发出请求,统一错误解析 * * @param string $type * @param array $data * @param bool $isPost * * @return array */ private function _request($type, $data = array(), $isPost = false) { // 需要 appToken if (!$data['appToken'] && !in_array($type, $this->noNeedAppToken)) { $data['appToken'] = $this->getAppToken(); } // 需要 accessToken if (!$data['accessToken'] && !in_array($type, $this->noNeedAccessToken)) { $data['accessToken'] = $this->getAccessToken(); } // 请求地址 $requestUrl = $this->apiHost . $type; // 指定请求的超时时间 $httpRequest = new HTTP_Request($requestUrl, HTTP_Request::METHOD_GET, array('connect_timeout' => 1, 'timeout' => 2)); // 文件上传 if ($type == '/v1/thread/upload') { $httpRequest->addFileParameter('pic', $data['pic']); unset($data['pic']); } // post数据 if ($isPost) { $httpRequest->setMethod(HTTP_Request::METHOD_POST); foreach ($data as $name => $value) { $httpRequest->addPostParameter($name, $value); } } else { $httpRequest->setUrl($requestUrl . '?' . http_build_query($data)); } // 发送请求 try { $response = $httpRequest->send(); $result = json_decode($response->getBody(), true); } catch (Exception $e) { throw new Exception($e->getMessage(), $e->getCode()); } if ($response->getStatus() != 200) { throw new Exception('接口请求失败', $response->getStatus()); return false; } if ($result['errCode']) { throw new Exception($result['errMsg'], $result['errCode']); return false; } return $result['data']; }