/** * Set Values to the class members * * @param Response $response */ private function setParams(Response $response) { $this->protocol = $response->getProtocolVersion(); $this->statusCode = (int) $response->getStatusCode(); $this->headers = $response->getHeaders(); $this->body = json_decode($response->getBody()->getContents()); $this->extractBodyParts(); }
/** * @param Response $response */ function out(Response $response) { header(sprintf('%s %s %s', $response->getProtocolVersion(), $response->getStatusCode(), $response->getReasonPhrase())); foreach ($response->getHeaders() as $name => $values) { foreach ($values as $value) { header(sprintf('%s: %s', $name, $value), false); } } stream_copy_to_stream(\GuzzleHttp\Psr7\StreamWrapper::getResource($response->getBody()), fopen('php://output', 'w')); }
protected function getSerializedResponse(Response $response) { $cached = new \SplFixedArray(5); $cached[0] = $response->getStatusCode(); $cached[1] = $response->getHeaders(); $cached[2] = $response->getBody()->__toString(); $cached[3] = $response->getProtocolVersion(); $cached[4] = $response->getReasonPhrase(); return serialize($cached); }
/** * Executes a Psr\Http\Message\RequestInterface * * @param Google_Client $client * @param Psr\Http\Message\RequestInterface $request * @return array decoded result * @throws Google_Service_Exception on server side error (ie: not authenticated, * invalid or malformed post body, invalid url) */ public static function doExecute(ClientInterface $client, RequestInterface $request, $expectedClass = null) { try { $httpHandler = HttpHandlerFactory::build($client); $response = $httpHandler($request); } catch (RequestException $e) { // if Guzzle throws an exception, catch it and handle the response if (!$e->hasResponse()) { throw $e; } $response = $e->getResponse(); // specific checking for Guzzle 5: convert to PSR7 response if ($response instanceof \GuzzleHttp\Message\ResponseInterface) { $response = new Response($response->getStatusCode(), $response->getHeaders() ?: [], $response->getBody(), $response->getProtocolVersion(), $response->getReasonPhrase()); } } return self::decodeHttpResponse($response, $request, $expectedClass); }
/** * Will send the response, standard PHP way * * @param Response $response * @return null */ public function sendResponse($response) { header("HTTP/" . $response->getProtocolVersion() . " " . $response->getStatusCode() . " " . $response->getReasonPhrase()); foreach ($response->getHeaders() as $name => $values) { foreach ($values as $value) { header(sprintf('%s: %s', $name, $value), false); } } $body = $response->getBody(); while (!$body->eof()) { $buf = $body->read(1048576); // Using a loose equality here to match on '' and false. if ($buf == null) { break; } else { echo $buf; } } }
public function testCanGiveCustomProtocolVersion() { $r = new Response(200, [], null, '1000'); $this->assertEquals('1000', $r->getProtocolVersion()); }
/** * Cache response * * @param RequestInterface $request * @param Response $response * @param int $ttl * * @return booelan */ protected function cacheResponse(RequestInterface $request, Response $response, $ttl = null) { if (!$this->isSupportedMethod($request)) { return; } // copy response in array to store $cached = new \SplFixedArray(5); $cached[0] = $response->getStatusCode(); $cached[1] = $response->getHeaders(); $cached[2] = $response->getBody()->__toString(); $cached[3] = $response->getProtocolVersion(); $cached[4] = $response->getReasonPhrase(); return $this->cache->set(self::getKey($request), serialize($cached), $ttl ?: $this->getCachettl($response)); }
public function testCanConstructWithProtocolVersion() { $r = new Response(200, [], null, '1000'); $this->assertSame('1000', $r->getProtocolVersion()); }