/** * 上传 * @param Response $authResponse * @param $filePath * @return UploadResponse * @throws DeepInException */ protected function upload(Response $authResponse, $filePath) { $authResponseValue = $authResponse->getResponseValue(); if (!$authResponseValue instanceof ResponseAuthValue) { throw new DeepInException("必须使用ResponseAuthValue作为格式化对象~!"); } $params = $authResponseValue->getPostBody(); $params['file'] = new \CURLFile($filePath); $request = new Request($authResponseValue->getPostUrl(), HttpMethod::POST, RequestBodyEncodeType::FORM_DATA); $this->curl = new Curl($request); $request->addParams($params); $response = $this->curl->makeRequest(new ResponseUploadValue()); // 检查返回 try { $response->check(); } catch (CurlException $e) { throw new DeepInException($e->getMessage()); } $upYunResponse = $this->getUpYunUrl($authResponseValue->getResourceUrl()); return new UploadResponse($authResponseValue, $response->getResponseValue(), $upYunResponse->getResponseValue()); }
/** * 调用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; }