本文整理汇总了PHP中GuzzleHttp\Message\Response::createRequest方法的典型用法代码示例。如果您正苦于以下问题:PHP Response::createRequest方法的具体用法?PHP Response::createRequest怎么用?PHP Response::createRequest使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类GuzzleHttp\Message\Response
的用法示例。
在下文中一共展示了Response::createRequest方法的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());
}
}