/** * 配信サーバへリクエストを送信する. * * @param string $mode * @param array $arrParams 追加パラメータ.連想配列で渡す. * @return string|object レスポンスボディ|エラー時にはPEAR::Errorオブジェクトを返す. */ function request($mode, $arrParams = array(), $arrCookies = array()) { $objReq = new HTTP_Request(); $objReq->setUrl(OSTORE_URL . 'upgrade/index.php'); $objReq->setMethod('POST'); $objReq->addPostData('mode', $mode); $objReq->addPostDataArray($arrParams); foreach ($arrCookies as $cookie) { $objReq->addCookie($cookie['name'], $cookie['value']); } $e = $objReq->sendRequest(); if (PEAR::isError($e)) { return $e; } else { return $objReq; } }
/** * _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']; }