本文整理汇总了PHP中Guzzle\Http\Message\Response::getHeader方法的典型用法代码示例。如果您正苦于以下问题:PHP Response::getHeader方法的具体用法?PHP Response::getHeader怎么用?PHP Response::getHeader使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Guzzle\Http\Message\Response
的用法示例。
在下文中一共展示了Response::getHeader方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getRateReset
/**
* Seconds remaining until a new time window opens
*
* @return null
*/
public function getRateReset()
{
if (!$this->lastResponse) {
return null;
}
/** @var Header $limit */
$limit = $this->lastResponse->getHeader('X-Rate-Reset');
return $limit ? $limit->normalize() : null;
}
示例2: testOnRequestSuccess
public function testOnRequestSuccess()
{
$event = new Event();
$response = new Response(200, array('X-RateLimit-Limit' => array(30), 'X-RateLimit-Remaining' => array(29), 'X-RateLimit-Reset' => array(10)));
$event['response'] = $response;
$plugin = new RateLimitPlugin();
$plugin->onRequestSuccess($event);
$this->assertAttributeEquals((string) $response->getHeader('X-RateLimit-Limit'), 'rateLimitMax', $plugin);
$this->assertAttributeEquals((string) $response->getHeader('X-RateLimit-Remaining'), 'rateLimitRemaining', $plugin);
$this->assertAttributeEquals((string) $response->getHeader('X-RateLimit-Reset'), 'rateLimitReset', $plugin);
$this->assertAttributeEquals(true, 'rateLimitEnabled', $plugin);
}
示例3: getLatestResponseHeaders
/**
* {@inheritdoc}
*/
public function getLatestResponseHeaders()
{
if (null === $this->response) {
return;
}
return ['reset' => (int) (string) $this->response->getHeader('RateLimit-Reset'), 'remaining' => (int) (string) $this->response->getHeader('RateLimit-Remaining'), 'limit' => (int) (string) $this->response->getHeader('RateLimit-Limit')];
}
示例4: getApiLimit
public static function getApiLimit(Response $response)
{
$remainingCalls = $response->getHeader('X-RateLimit-Remaining');
if (null !== $remainingCalls && 1 > $remainingCalls) {
throw new ApiLimitExceedException($remainingCalls);
}
}
示例5: cache
public function cache(RequestInterface $request, Response $response)
{
$currentTime = time();
$ttl = $request->getParams()->get('cache.override_ttl') ?: $response->getMaxAge() ?: $this->defaultTtl;
if ($cacheControl = $response->getHeader('Cache-Control')) {
$stale = $cacheControl->getDirective('stale-if-error');
$ttl += $stale == true ? $ttl : $stale;
}
// Determine which manifest key should be used
$key = $this->getCacheKey($request);
$persistedRequest = $this->persistHeaders($request);
$entries = array();
if ($manifest = $this->cache->fetch($key)) {
// Determine which cache entries should still be in the cache
$vary = $response->getVary();
foreach (unserialize($manifest) as $entry) {
// Check if the entry is expired
if ($entry[4] < $currentTime) {
continue;
}
$entry[1]['vary'] = isset($entry[1]['vary']) ? $entry[1]['vary'] : '';
if ($vary != $entry[1]['vary'] || !$this->requestsMatch($vary, $entry[0], $persistedRequest)) {
$entries[] = $entry;
}
}
}
// Persist the response body if needed
$bodyDigest = null;
if ($response->getBody() && $response->getBody()->getContentLength() > 0) {
$bodyDigest = $this->getBodyKey($request->getUrl(), $response->getBody());
$this->cache->save($bodyDigest, (string) $response->getBody(), $ttl);
}
array_unshift($entries, array($persistedRequest, $this->persistHeaders($response), $response->getStatusCode(), $bodyDigest, $currentTime + $ttl));
$this->cache->save($key, serialize($entries));
}
示例6: validateResponse
/**
* Validate the HTTP response and throw exceptions on errors
*
* @throws ServerException
*/
private function validateResponse()
{
if ($this->httpResponse->getStatusCode() !== 200) {
$statusCode = $this->httpResponse->getStatusCode();
throw new ServerException('Server responded with HTTP status ' . $statusCode, $statusCode);
} else {
if (strpos($this->httpResponse->getHeader('Content-Type'), 'application/json') === false) {
throw new ServerException('Server did not respond with the expected content-type (application/json)');
}
}
try {
$this->httpResponse->json();
} catch (RuntimeException $e) {
throw new ServerException($e->getMessage());
}
}
示例7: parseHeaders
/**
* Parses additional exception information from the response headers
*
* @param RequestInterface $request Request that was issued
* @param Response $response The response from the request
* @param array $data The current set of exception data
*/
protected function parseHeaders(RequestInterface $request, Response $response, array &$data)
{
$data['message'] = $response->getStatusCode() . ' ' . $response->getReasonPhrase();
if ($requestId = $response->getHeader('x-amz-request-id')) {
$data['request_id'] = $requestId;
$data['message'] .= " (Request-ID: {$requestId})";
}
}
示例8: decode
public static function decode(Response $response)
{
if (strpos($response->getHeader(Header::CONTENT_TYPE), Mime::JSON) !== false) {
$string = (string) $response->getBody();
$response = json_decode($string);
self::checkJsonError($string);
return $response;
}
}
示例9: getDelay
/**
* {@inheritdoc}
*/
protected function getDelay($retries, RequestInterface $request, Response $response = null, HttpException $e = null)
{
if ($response) {
// Validate the checksum against our computed checksum
if ($checksum = (string) $response->getHeader('x-amz-crc32')) {
// Retry the request if the checksums don't match, otherwise, return null
return $checksum != hexdec(Stream::getHash($response->getBody(), 'crc32b')) ? true : null;
}
}
}
示例10: getDelay
protected function getDelay($retries, RequestInterface $request, Response $response = null, HttpException $e = null)
{
if ($response) {
//Short circuit the rest of the checks if it was successful
if ($response->isSuccessful()) {
return false;
} else {
if (isset($this->errorCodes[$response->getStatusCode()])) {
if ($response->getHeader("Retry-After")) {
return $response->getHeader("Retry-After")->__toString();
} else {
return self::$defaultRetryAfter;
}
} else {
return null;
}
}
}
}
示例11: parse
/**
* {@inheritdoc}
*/
public function parse(Response $response)
{
$data = array('code' => null, 'message' => null, 'type' => $response->isClientError() ? 'client' : 'server', 'request_id' => (string) $response->getHeader('x-amzn-RequestId'), 'parsed' => null);
if (null !== ($json = json_decode($response->getBody(true), true))) {
$data['parsed'] = $json;
$json = array_change_key_case($json);
$data = $this->doParse($data, $json);
}
return $data;
}
示例12: getContextFromResponse
private function getContextFromResponse(Response $response)
{
$extraFields = array();
$headersToLookFor = array('x-served-by', 'x-backend', 'x-location', 'x-varnish');
foreach ($headersToLookFor as $headerName) {
if ($response->hasHeader($headerName)) {
$extraFields[$headerName] = (string) $response->getHeader($headerName);
}
}
return $extraFields;
}
示例13: factory
public static function factory(RequestInterface $request, Response $response)
{
$label = 'Bearer error response';
$bearerReason = self::headerToReason($response->getHeader("WWW-Authenticate"));
$message = $label . PHP_EOL . implode(PHP_EOL, array('[status code] ' . $response->getStatusCode(), '[reason phrase] ' . $response->getReasonPhrase(), '[bearer reason] ' . $bearerReason, '[url] ' . $request->getUrl()));
$e = new static($message);
$e->setResponse($response);
$e->setRequest($request);
$e->setBearerReason($bearerReason);
return $e;
}
示例14: getBackoffPeriod
/**
* Get the amount of time to delay in seconds before retrying a request
*
* @param int $retries Number of retries of the request
* @param RequestInterface $request Request that was sent
* @param Response $response Response that was received. Note that there may not be a response
* @param HttpException $e Exception that was encountered if any
*
* @return bool|int Returns false to not retry or the number of seconds to delay between retries
*/
public function getBackoffPeriod($retries, RequestInterface $request, Response $response = null, HttpException $e = null)
{
if (!$response) {
return false;
}
if ($response->getStatusCode() != 429) {
return false;
}
$reset = (string) $response->getHeader('X-Rate-Limit-Reset');
if (!preg_match('/^[0-9]+$/', $reset)) {
return false;
}
return ((int) $reset + 0.1) * $this->getMultiplier();
}
示例15: doParse
/**
* {@inheritdoc}
*/
protected function doParse(array $data, Response $response)
{
// Merge in error data from the JSON body
if ($json = $data['parsed']) {
$data = array_replace($data, $json);
}
// Correct error type from services like Amazon Glacier
if (!empty($data['type'])) {
$data['type'] = strtolower($data['type']);
}
// Retrieve the error code from services like Amazon Elastic Transcoder
if ($code = (string) $response->getHeader('x-amzn-ErrorType')) {
$data['code'] = substr($code, 0, strpos($code, ':'));
}
return $data;
}