public function dump($extended = false) { if ($extended) { dump($this->response->getStatusCode()); dump($this->response->getHeaders()); } dd($this->result); }
/** * @return ErrorResponse|SuccessResponse */ public function deduce() { /* @var array $response */ $response = (array) json_decode($this->response->getBody()->getContents()); if (array_key_exists('type', $response) && $response['type'] === 'error') { return new ErrorResponse($this->response->getStatusCode(), $this->response->getHeaders(), $this->response->getBody()); } return new SuccessResponse($this->response->getStatusCode(), $this->response->getHeaders(), $this->response->getBody()); }
protected function isError(Response $response) { if (\array_get($response->getHeaders(), 'RETS-Error', [null])[0] == 1) { return true; } $content_type = \array_get($response->getHeaders(), 'Content-Type', [null])[0]; if ($content_type and strpos($content_type, 'xml') !== false) { return true; } return false; }
/** * Returns the SOAP headers from the last response. * * @return mix The last SOAP response headers. */ public function lastResponseHeaders() { if ($this->response instanceof Response) { return $this->response->getHeaders(); } return []; }
/** * Get the response headers as an array. * * @return array * @throws \Hedii\ZoteroApi\Exceptions\BadMethodCallException */ public function getHeaders() { if (!$this->response) { throw new BadMethodCallException('Cannot call getHeaders() on null'); } return $this->response->getHeaders(); }
function it_should_encode_a_responses_collection() { $response = new Response(200); $responses = array($response); $format = ['code' => $response->getStatusCode(), 'headers' => $response->getHeaders(), 'body' => (string) $response->getBody()]; $formatted = json_encode(array($format), JSON_PRETTY_PRINT); $this->encodeResponsesCollection($responses)->shouldEqual($formatted); }
/** * 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(); }
/** * Attempt to create local response type from guzzle response * * @param GuzzleResponse $guzzleResponse * * @return Response */ protected static function createLocalResponse(GuzzleResponse $guzzleResponse) { $response = new Response($guzzleResponse->getBody(), $guzzleResponse->getStatusCode()); $headers = $guzzleResponse->getHeaders(); array_walk($headers, function ($values, $name) use($response) { $response->header($name, implode(', ', $values), true); }); return $response; }
/** * ServerResponseException constructor. * * @param GuzzleResponse $response */ public function __construct(GuzzleResponse $response) { $code = $response->getStatusCode(); $responseHeader = $response->getHeaders(); $responseBody = $response->getBody()->getContents(); if (array_keys($responseHeader, "Retry-After")) { $this->retryAfter = $responseHeader["Retry-After"]; } parent::__construct($responseBody, $code); }
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); }
/** * @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')); }
private function formatResponse(Response $response) { $headers = array(); foreach ($response->getHeaders() as $key => $values) { foreach ($values as $value) { $headers[] = sprintf('%s: %s', $key, $value); } } return sprintf("%s\n\n%s", implode("\n", $headers), $response->getBody()); }
/** * */ public function completed() { //echo '#'; $response = new Response($this->response->getStatusCode(), $this->response->getHeaders(), $this->buffer); foreach ($this->observers as $observer) { /* @var \Rx\ObserverInterface $observer */ $observer->onNext($response); $observer->onCompleted(); } $this->onCompleted(); $this->buffer = ""; }
/** * 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; } } }
/** * Taken from Mink\BrowserKitDriver * * @param Response $response * * @return \Symfony\Component\BrowserKit\Response */ protected function createResponse(Psr7Response $response) { $body = (string) $response->getBody(); $headers = $response->getHeaders(); $contentType = null; if (isset($headers['Content-Type'])) { $contentType = reset($headers['Content-Type']); } if (!$contentType) { $contentType = 'text/html'; } if (strpos($contentType, 'charset=') === false) { if (preg_match('/\\<meta[^\\>]+charset *= *["\']?([a-zA-Z\\-0-9]+)/i', $body, $matches)) { $contentType .= ';charset=' . $matches[1]; } $headers['Content-Type'] = [$contentType]; } $status = $response->getStatusCode(); if ($status < 300 || $status >= 400) { $matches = []; $matchesMeta = preg_match('/\\<meta[^\\>]+http-equiv="refresh" content="(\\d*)\\s*;?\\s*url=(.*?)"/i', $body, $matches); if (!$matchesMeta && isset($headers['Refresh'])) { // match by header preg_match('~(\\d*);?url=(.*)~', (string) reset($headers['Refresh']), $matches); } if (!empty($matches) && (empty($matches[1]) || $matches[1] < $this->refreshMaxInterval)) { $uri = new Psr7Uri($this->getAbsoluteUri($matches[2])); $currentUri = new Psr7Uri($this->getHistory()->current()->getUri()); if ($uri->withFragment('') != $currentUri->withFragment('')) { $status = 302; $headers['Location'] = $matchesMeta ? htmlspecialchars_decode($uri) : (string) $uri; } } } return new BrowserKitResponse($body, $status, $headers); }
private function makeData(Response $serverResponse) { return ['headers' => $serverResponse->getHeaders(), 'status' => ['code' => $serverResponse->getStatusCode(), 'message' => $serverResponse->getReasonPhrase()], 'body' => json_decode($serverResponse->getBody()), 'cache' => ['enabled' => false, 'created_at' => null, 'end_at' => null]]; }
/** * 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)); }
/** * Create an array of the response object * * @param Response $response * @return array */ private function buildResponse(Response $response) { $this->response['body'] = $response->getBody()->getContents(); $this->response['size'] = $response->getBody()->getSize(); $this->response['status_code'] = $response->getStatusCode(); //Build the response headers $headers = $response->getHeaders(); foreach ($headers as $name => $value) { $this->response['headers'][$name] = $value; } return $this->response; }
/** * * @return string */ public function getHeaders() { return $this->response->getHeaders(); }
public function getDebugInfos(Response $response) { $lastRequest = $this->getLastRequestMiddleware()->getLastRequest(); return json_encode(['response' => ['status' => $response->getStatusCode(), 'body' => $response->getBody()->__toString(), 'header' => $response->getHeaders()], 'request' => ['uri' => $lastRequest->getUri()->__toString(), 'method' => $lastRequest->getMethod(), 'body' => $lastRequest->getBody()->__toString(), 'headers' => $lastRequest->getHeaders()]]); }
/** * Get the status string for the given response * * @param Response $response * * @return mixed */ protected function getUnreachableStatus(Response $response) { switch ($response->getStatusCode()) { case 200: return false; case 301: case 302: $headers = $response->getHeaders(); return $headers['Location'][0]; } return true; }
public function testWithoutHeaderThatExists() { $r = new Response(200, ['Foo' => 'Bar', 'Baz' => 'Bam']); $r2 = $r->withoutHeader('foO'); $this->assertTrue($r->hasHeader('foo')); $this->assertSame(['Foo' => ['Bar'], 'Baz' => ['Bam']], $r->getHeaders()); $this->assertFalse($r2->hasHeader('foo')); $this->assertSame(['Baz' => ['Bam']], $r2->getHeaders()); }