/** * {@inheritdoc} */ public function toJson(array $config = array()) { $body = (string) $this->getBody(); if (!$body) { return; } return $this->_delegate->json($config); }
/** * Factory method to create a new exception with a normalized error message * * @param puzzle_message_RequestInterface $request Request * @param puzzle_message_ResponseInterface $response Response received * @param Exception $previous Previous exception * * @return self */ public static function create(puzzle_message_RequestInterface $request, puzzle_message_ResponseInterface $response = null, Exception $previous = null) { if (!$response) { return new self('Error completing request', $request, null, $previous); } $statusCode = $response->getStatusCode(); $level = $statusCode[0]; if ($level == '4') { $label = 'Client error response'; $className = 'puzzle_exception_ClientException'; } elseif ($level == '5') { $label = 'Server error response'; $className = 'puzzle_exception_ServerException'; } else { $label = 'Unsuccessful response'; $className = __CLASS__; } $message = $label . ' [url] ' . $request->getUrl() . ' [status code] ' . $response->getStatusCode() . ' [reason phrase] ' . $response->getReasonPhrase(); return new $className($message, $request, $response, $previous); }
/** * Set the appropriate URL on the request based on the location header * * @param puzzle_message_RequestInterface $redirectRequest * @param puzzle_message_ResponseInterface $response */ private function setRedirectUrl(puzzle_message_RequestInterface $redirectRequest, puzzle_message_ResponseInterface $response) { $location = $response->getHeader('Location'); $location = puzzle_Url::fromString($location); // Combine location with the original URL if it is not absolute. if (!$location->isAbsolute()) { $originalUrl = puzzle_Url::fromString($redirectRequest->getUrl()); // Remove query string parameters and just take what is present on // the redirect Location header $originalUrl->getQuery()->clear(); $location = $originalUrl->combine($location); } $redirectRequest->setUrl($location); }
public function extractCookies(puzzle_message_RequestInterface $request, puzzle_message_ResponseInterface $response) { if ($cookieHeader = $response->getHeader('Set-Cookie', true)) { foreach ($cookieHeader as $cookie) { $sc = puzzle_cookie_SetCookie::fromString($cookie); if (!$sc->getDomain()) { $sc->setDomain($request->getHost()); } $this->setCookie($sc); } } }