コード例 #1
0
 /**
  * 调用API接口
  * @param $apiName
  * @param $httpMethod
  * @param array $params
  * @param null $request
  * @return \Simple\Curl\Response\Response
  * @throws DeepInException
  */
 public function api($apiName, $httpMethod, $params = array(), $request = null)
 {
     $url = $this->serverName . $apiName;
     $httpMethod = strtoupper($httpMethod);
     if (!$request instanceof Request) {
         $request = new Request($url, $httpMethod, RequestBodyEncodeType::FORM_DATA);
         $request->addHeaders(array("Content-Type" => "application/json", "Expect" => ''));
     }
     $request->addParams($params);
     $curl = new Curl($request);
     $response = $curl->makeRequest(new OpenAPIResponseValue());
     if ($response->getErrorCode() != 0) {
         throw new DeepInException("请求" . $url . '失败,失败原因:' . $response->getErrorMsg());
     }
     $httpStatus = $response->getHttpCode();
     if (in_array(intval($httpStatus / 100), array(4, 5))) {
         throw new DeepInException("Api server返回http状态码为:" . $httpStatus);
     }
     return $response;
 }
コード例 #2
0
 /**
  * 发出请求
  * @param $url
  * @param $params
  * @param $cookie
  * @param string $method
  * @param string $protocol
  * @return \Simple\Curl\Response\ResponseValue
  * @throws DeepInException
  */
 private function makeRequest($url, $params, $cookie, $method = "POST", $protocol = 'http')
 {
     $request = new Request($url, $method, RequestBodyEncodeType::JSON);
     $request->addHeaders(array("Expect" => "", "Access-Token" => $this->hookClient->getToken(), "Content-Type" => "application/json"));
     foreach ($params as $key => $val) {
         $request->setParam($key, $val);
     }
     foreach ($cookie as $key => $val) {
         $request->setCookie($key, $val);
     }
     $curl = new Curl($request);
     $response = $curl->makeRequest(new HookResponseValue());
     if ($response->getErrorCode() != 0) {
         throw new DeepInException($response->getErrorMsg());
     }
     if (in_array(intval($response->getHttpCode() / 100), array(4, 5))) {
         throw new DeepInException("hook server返回状态码为" . $response->getHttpCode());
     }
     return $response->getResponseValue()->getData();
 }