public function testCanConstructWithHeadersAsArray() { $r = new Response(200, ['Foo' => ['baz', 'bar']]); $this->assertSame(['Foo' => ['baz', 'bar']], $r->getHeaders()); $this->assertSame('baz, bar', $r->getHeaderLine('Foo')); $this->assertSame(['baz', 'bar'], $r->getHeader('Foo')); }
static function retryDecider($retries, Request $request, Response $response = null, RequestException $exception = null) { // Limit the number of retries to 5 if ($retries >= 5) { return false; } // Retry connection exceptions if ($exception instanceof ConnectException) { return true; } if ($response) { // Retry on server errors if ($response->getStatusCode() >= 500) { return true; } // Retry on rate limits if ($response->getStatusCode() == 429) { $retryDelay = $response->getHeaderLine('Retry-After'); if (strlen($retryDelay)) { printf(" retry delay: %d secs\n", (int) $retryDelay); sleep((int) $retryDelay); return true; } } } return false; }
/** * Check if we got an expected response * * @param Node $node * @param Response $response * @return boolean */ protected function hasExpectedResponse(Node $node, Response $response) { $status = $response->getStatusCode(); $contentType = preg_replace('/\\s*;.*$/', '', $response->getHeaderLine('Content-Type')); if ($status == 404) { return false; } elseif ($status >= 300 || !in_array($contentType, ['application/json', 'text/plain'])) { $url = $this->getUrl($node); if ($contentType === 'text/plain') { $message = $response->getBody(); } else { $message = "Server responded with a {$status} status and {$contentType}"; } trigger_error("Failed to fetch '{$url}': {$message}", E_USER_WARNING); return false; } return true; }
public function testCanSetHeaderAsArray() { $r = new Response(200, ['foo' => ['baz ', ' bar ']]); $this->assertEquals('baz, bar', $r->getHeaderLine('foo')); $this->assertEquals(['baz', 'bar'], $r->getHeader('foo')); }
/** * Get X-CSRFToken from Guzzle response * * @param \GuzzleHttp\Psr7\Response $response * @return mixed * @throws \InstagramWrapper\InstagramException */ protected function getCsrftoken(Response $response) { $cookies_string = $response->getHeaderLine('set-cookie'); preg_match("/csrftoken=(.*?);/", $cookies_string, $csrftoken_raw); if (empty($csrftoken_raw[1])) { throw new InstagramException("Can't get csrftoken from response"); } $csrftoken = $csrftoken_raw[1]; return $csrftoken; }
/** * Attempt to extract a Solr error message from a response. * * @param Response $response The response. * @return string Error message or NULL if not found. */ private function getSolrErrorMessage(Response $response) { // Try to get the SOLR error message from the response body $contentType = $response->getHeaderLine("Content-Type"); $content = $response->getBody()->getContents(); // If response contains XML if (strpos($contentType, 'application/xml') !== false) { return $this->getXmlError($content); } // If response contains JSON if (strpos($contentType, 'application/json') !== false) { return $this->getJsonError($content); } // Message not found return null; }
/** * Get HTTP header. * * @param $key * @param mixed $default * * @return mixed */ public function header($key, $default = null) { return $this->response->hasHeader($key) ? $this->response->getHeaderLine($key) : $default; }
/** * @return mixed */ public function body(Response $response) { $type = $response->getHeaderLine('Content-Type'); if (false === strpos($type, 'application/json')) { throw new \RuntimeException(sprintf('Invalid response type `%s` detected', $type)); } $body = (string) $response->getBody(); return json_decode($body, true); }
/** * 从响应中提取需要catch的参数 * @param Examination $examination * @param Response $response * @return bool * @throws InvalidArgumentException */ protected function extractArguments(Examination $examination, Response $response) { $catch = $examination->getCatch(); if (empty($catch)) { return true; } //从header里面提取 if (isset($catch['header']) && is_array($catch['header'])) { foreach ($catch['header'] as $parameter => $name) { if (is_numeric($parameter)) { $newArgumentName = $name; $oldArgumentName = $name; } else { $newArgumentName = $name; $oldArgumentName = $parameter; } $this->arguments->set($newArgumentName, $response->getHeaderLine($oldArgumentName)); } } //从body里面提取 if (isset($catch['body']) && is_array($catch['body'])) { $json = json_decode($response->getBody(), true); if (json_last_error() != JSON_ERROR_NONE) { throw new InvalidArgumentException(sprintf("Invalid Json Format")); } foreach ($catch['body'] as $parameter => $name) { if (is_numeric($parameter)) { $newArgumentName = $name; $oldArgumentName = $name; } else { $newArgumentName = $name; $oldArgumentName = $parameter; } $this->arguments->set($newArgumentName, Hash::get($json, $oldArgumentName)); } } return true; }