本文整理汇总了PHP中GuzzleHttp\Psr7\Response::getHeaderLine方法的典型用法代码示例。如果您正苦于以下问题:PHP Response::getHeaderLine方法的具体用法?PHP Response::getHeaderLine怎么用?PHP Response::getHeaderLine使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类GuzzleHttp\Psr7\Response
的用法示例。
在下文中一共展示了Response::getHeaderLine方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testCanConstructWithHeadersAsArray
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'));
}
示例2: retryDecider
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;
}
示例3: hasExpectedResponse
/**
* 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;
}
示例4: testCanSetHeaderAsArray
public function testCanSetHeaderAsArray()
{
$r = new Response(200, ['foo' => ['baz ', ' bar ']]);
$this->assertEquals('baz, bar', $r->getHeaderLine('foo'));
$this->assertEquals(['baz', 'bar'], $r->getHeader('foo'));
}
示例5: getCsrftoken
/**
* 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;
}
示例6: getSolrErrorMessage
/**
* 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;
}
示例7: header
/**
* 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;
}
示例8: body
/**
* @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);
}
示例9: extractArguments
/**
* 从响应中提取需要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;
}