/** * Returns object with properties int:status object:body * @param string $method * @param string $path * @param array $query * @param bool $doAuth * @throws \InvalidArgumentException * @throws \Exception * @return \stdClass */ public function request($method, $path, $query = array(), $doAuth = false) { $this->userAgent = 'Rocker REST Client v' . Server::VERSION; $method = strtolower($method); $request = $this->initiateRequest($method, $path, $query); if ($doAuth) { $this->addAuthHeader($request); } try { $this->lastResponse = $request->send(); } catch (\Guzzle\Http\Exception\ClientErrorResponseException $e) { $this->lastResponse = $e->getResponse(); if ($this->lastResponse->getStatusCode() == 401 && !$doAuth && !empty($this->user)) { trigger_error('Doing unauthenticated requests to an URI that requires authentication (' . $path . ')', E_WARNING); return $this->request($method, $path, $query, true); } } if ($this->lastResponse->getStatusCode() == 400) { throw new ClientException($this->lastResponse, 400); } if ($this->lastResponse->getStatusCode() == 204) { return (object) array('status' => 204, 'body' => array()); } if (strpos($this->lastResponse->getContentType(), 'json') === false) { throw new ClientException($this->lastResponse, ClientException::ERR_UNEXPECTED_CONTENT_TYPE, 'Server responded with unexpected content type (' . $this->lastResponse->getContentType() . ')'); } $str = (string) $this->lastResponse->getBody(); $body = json_decode($str); return (object) array('status' => $this->lastResponse->getStatusCode(), 'headers' => $this->headerCollectionToArray($this->lastResponse->getHeaders()), 'body' => $body); }
public function __construct(\Guzzle\Http\Message\Response $response, $type) { $this->_response = $response; // if json returned - parse and fill structure $contentType = $this->_response->getContentType(); if (strpos($contentType, ';')) { list($contentType, ) = explode(';', $contentType); } if ($contentType == 'application/json') { $this->_structure = new $type($this->_response->json()); } else { throw new \Exception('Structure parser for content type "' . $this->_response->getContentType() . '" not implemented'); } }
/** * Parses response into an array * * @param Response $response * @return array */ protected function parseResponseIntoArray($response) { if (strpos($response->getContentType(), 'json') === false) { parse_str($response->getBody(true), $array); return $array; } return $response->json(); }
/** * Reads response meta tags to guess content-type charset. */ protected function createResponse(GuzzleResponse $response) { $body = $response->getBody(true); $contentType = $response->getContentType(); if (!$contentType || false === strpos($contentType, 'charset=')) { if (preg_match('/\\<meta[^\\>]+charset *= *["\']?([a-zA-Z\\-0-9]+)/i', $body, $matches)) { $contentType .= ';charset=' . $matches[1]; } } $response->setHeader('Content-Type', $contentType); return parent::createResponse($response); }
/** * Reads response meta tags to guess content-type charset. */ protected function createResponse(GuzzleResponse $response) { $body = $response->getBody(true); $statusCode = $response->getStatusCode(); $headers = $response->getHeaders()->getAll(); $contentType = $response->getContentType(); if (!$contentType || false === strpos($contentType, 'charset=')) { if (preg_match('/\\<meta[^\\>]+charset *= *["\']?([a-zA-Z\\-0-9]+)/i', $body, $matches)) { $contentType .= ';charset=' . $matches[1]; } } $headers['Content-Type'] = $contentType; return new Response($body, $statusCode, $headers); }
/** * Factory method to create a new response exception based on the response code. * * @param RequestInterface $request Request * @param Response $response Response received * @param string $label * * @return BadResponseException */ public static function factory(RequestInterface $request, Response $response, $label = null) { if (!$label) { if ($response->isClientError()) { $label = 'Client error response'; } elseif ($response->isServerError()) { $label = 'Server error response'; } else { $label = 'Unsuccessful response'; } } $message = $label . PHP_EOL . implode(PHP_EOL, array('[status code] ' . $response->getStatusCode(), '[reason phrase] ' . $response->getReasonPhrase(), '[url] ' . $request->getUrl(), '[content type] ' . $response->getContentType(), '[response body] ' . $response->getBody(true))); $result = new static($message); $result->setResponse($response); $result->setRequest($request); return $result; }
/** * @return bool */ private function isHttpErrorFatal(Response $response, $expectedResponseContentType) { return $response->isSuccessful() && $response->getContentType() != $expectedResponseContentType; }
private function parseResponse(Response $response, $path) { $statusCode = $response->getStatusCode(); if ($statusCode !== 200) { throw new UnexpectedValueException(sprintf('Expected status code 200 from "%s", got %d', $path, $statusCode)); } $contentType = $response->hasHeader('content-type') ? $response->getContentType() : ''; if (substr($contentType, 0, 10) !== 'text/plain') { throw new UnexpectedValueException(sprintf('Expected content type "text/plain" from "%s", got "%s"', $path, $contentType)); } return $this->parseRequestFromResponse($response, $path); }
/** * @param RequestInterface $request * @param Response $response * @return \Guzzle\Http\EntityBodyInterface|mixed|string */ public function handleResponse(RequestInterface $request, Response $response) { $body = $response->getBody(true); switch ($response->getContentType()) { case "application/json": $body = json_decode($body, true); break; } return [$response->getStatusCode(), $body]; }
/** * {@inheritdoc} */ public function getContentType() { return $this->response->getContentType(); }
function pushLinksForResponse(Guzzle\Http\Message\Response $resp) { $self = null; $hubs = []; $linkHeader = $resp->getHeader('link'); if ($linkHeader instanceof Guzzle\Http\Message\Header\Link) { $links = $linkHeader->getLinks(); foreach ($links as $link) { if (strpos(" {$link['rel']} ", ' self ') !== false) { $self = $link['url']; } if (strpos(" {$link['rel']} ", ' hub ') !== false) { $hubs[] = $link['url']; } } } if (strpos($resp->getContentType(), 'html') !== false) { $mf = Mf2\parse($resp->getBody(true), $resp->getEffectiveUrl()); if (!empty($mf['rels']['hub'])) { $hubs = array_merge($hubs, $mf['rels']['hub']); } if (!empty($mf['rels']['self']) and $self === null) { $self = $mf['rels']['self'][0]; } } return ['self' => $self, 'hub' => $hubs]; }