当前位置: 首页>>代码示例>>PHP>>正文


PHP Response::send方法代码示例

本文整理汇总了PHP中GuzzleHttp\Message\Response::send方法的典型用法代码示例。如果您正苦于以下问题:PHP Response::send方法的具体用法?PHP Response::send怎么用?PHP Response::send使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在GuzzleHttp\Message\Response的用法示例。


在下文中一共展示了Response::send方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: sendRequest

 /**
  * Making request using Guzzle
  *
  * @param string $method Type of request, either POST, GET, PUT or DELETE
  * @param string $endpoint The operation / task for API
  * @param array $data The parameter need to be passed
  * @param array $options The options like header, body, etc
  *
  * @return EntityBodyInterface|string
  * @throws \Exception
  */
 private function sendRequest($method, $endpoint, array $data = array(), array $options = array())
 {
     $uri = $this->buildUri($endpoint);
     if ($method === "GET" || $method === "PUT") {
         $uri = $this->buildUri($endpoint, $data);
     } elseif (count($data)) {
         $options['body'] = $data;
     }
     $this->request = $this->client->createRequest($method, $uri, $options);
     $this->response = $this->client->send($this->request);
     if ($this->response->getStatusCode() >= 400) {
         $bt = debug_backtrace();
         $caller = $bt[2];
         if (isset($caller['class']) && $caller['class'] === get_class(new StacklaModel())) {
             $json = (string) $this->response->getBody();
             if (JsonValidator::validate($json, true)) {
                 $content = json_decode($json, true);
                 if (isset($content['errors'])) {
                     $caller['object']->fromArray($content);
                 }
             }
         }
         if ($this->logger) {
             $this->logger->addError('-> REQUEST [' . $this->request->getMethod() . ' - ' . $this->request->getUrl() . "]", array($this->request->getMethod() !== "GET" ? (string) $this->request->getBody() : ""));
             $this->logger->addError('<- RESPONSE [' . $this->response->getStatusCode() . ':' . $this->response->getReasonPhrase() . "]", array((string) $this->response->getBody()));
         }
     } else {
         if ($this->logger) {
             $this->logger->addInfo('-> REQUEST [' . $this->request->getMethod() . ' - ' . $this->request->getUrl() . "]", array($this->request->getMethod() !== "GET" ? (string) $this->request->getBody() : ""));
             $this->logger->addInfo('<- RESPONSE [' . $this->response->getStatusCode() . ':' . $this->response->getReasonPhrase() . "]", array($this->response->json()));
         }
     }
     $statusCode = $this->response->getStatusCode();
     switch ($statusCode) {
         case 200:
             return (string) $this->response->getBody();
         case 400:
             throw ApiException::create(sprintf("Server return %s error code. Bad request: The request could not be understood. %s", $this->response->getStatusCode(), (string) $this->response->getBody()), $statusCode, (string) $this->response->getBody());
         case 401:
             throw ApiException::create(sprintf("Server return %s error code. Unauthorized: Authentication credentials invalid or not authorised to access resource", $this->response->getStatusCode()), $statusCode, (string) $this->response->getBody());
         case 403:
             throw ApiException::create(sprintf("\n                  Server return %s error code. Rate limit exceeded: Too many requests in the current time window", $this->response->getStatusCode()), $statusCode, (string) $this->response->getBody());
         case 404:
             throw ApiException::create(sprintf("Server return %s error code. Invalid resource: Invalid resource specified or resource not found", $this->response->getStatusCode()), $statusCode, (string) $this->response->getBody());
         default:
             throw ApiException::create(sprintf("Server return %s error code.Server error: An error on the server prohibited a successful response; please contact support. %s", $this->response->getStatusCode(), (string) $this->response->getBody()), $statusCode, (string) $this->response->getBody());
     }
 }
开发者ID:stackla,项目名称:stackla-php-sdk,代码行数:59,代码来源:Request.php


注:本文中的GuzzleHttp\Message\Response::send方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。