本文整理汇总了PHP中Guzzle\Http\Message\Response::hasHeader方法的典型用法代码示例。如果您正苦于以下问题:PHP Response::hasHeader方法的具体用法?PHP Response::hasHeader怎么用?PHP Response::hasHeader使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Guzzle\Http\Message\Response
的用法示例。
在下文中一共展示了Response::hasHeader方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: 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;
}
示例2: createExceptionFromStatusCode
/**
* @param string $errorName
* @param \Guzzle\Http\Message\Response $response
* @param int $statusCode
* @return \Phobetor\Billomat\Exception\ExceptionInterface
*/
public function createExceptionFromStatusCode($errorName, $response, $statusCode)
{
$exception = null;
switch ($statusCode) {
case self::STATUS_NOT_FOUND:
return new NotFoundException($errorName, $statusCode);
case self::STATUS_BAD_REQUEST:
return new BadRequestException($errorName, $statusCode);
case self::STATUS_UNAUTHORIZED:
return new UnauthorizedException($errorName, $statusCode);
case self::STATUS_TOO_MANY_REQUESTS:
$exception = new TooManyRequestsException($errorName, $statusCode);
if ($response->hasHeader('X-Rate-Limit-Remaining')) {
$exception->setRateLimitRemaining((int) (string) $response->getHeader('X-Rate-Limit-Remaining'));
}
if ($response->hasHeader('X-Rate-Limit-Reset')) {
$exception->setRateLimitReset((int) (string) $response->getHeader('X-Rate-Limit-Reset'));
}
return $exception;
default:
return new UnknownErrorException($errorName, $statusCode);
}
}
示例3: handle304Response
protected function handle304Response(RequestInterface $request, Response $validateResponse, Response $response)
{
static $replaceHeaders = array('Date', 'Expires', 'Cache-Control', 'ETag', 'Last-Modified');
if ($validateResponse->getEtag() != $response->getEtag()) {
return false;
}
$modified = false;
foreach ($replaceHeaders as $name) {
if ($validateResponse->hasHeader($name)) {
$modified = true;
$response->setHeader($name, $validateResponse->getHeader($name));
}
}
if ($modified && $this->canCache->canCacheResponse($response)) {
$this->storage->cache($request, $response);
}
return true;
}
示例4: onRequestBeforeSend
/**
* Check if a response in cache will satisfy the request before sending
*
* @param Event $event
*/
public function onRequestBeforeSend(Event $event)
{
$request = $event['request'];
if (!$this->canCache->canCacheRequest($request)) {
return;
}
$hashKey = $this->keyProvider->getCacheKey($request);
$this->cached[$request] = $hashKey;
// If the cached data was found, then make the request into a
// manually set request
if ($cachedData = $this->storage->fetch($hashKey)) {
unset($this->cached[$request]);
$response = new Response($cachedData[0], $cachedData[1], $cachedData[2]);
$response->setHeader('Age', time() - strtotime($response->getDate() ?: 'now'));
if (!$response->hasHeader('X-Guzzle-Cache')) {
$response->setHeader('X-Guzzle-Cache', "key={$hashKey}");
}
// Validate that the response satisfies the request
if ($this->canResponseSatisfyRequest($request, $response)) {
$request->setResponse($response);
}
}
}
示例5: onRequestBeforeSend
/**
* Check if a response in cache will satisfy the request before sending
*
* @param Event $event
*/
public function onRequestBeforeSend(Event $event)
{
$request = $event['request'];
// This request is being prepared
$key = spl_object_hash($request);
$hashKey = $this->getCacheKey($request);
$this->cached[$key] = $hashKey;
$cachedData = $this->getCacheAdapter()->fetch($hashKey);
// If the cached data was found, then make the request into a
// manually set request
if ($cachedData) {
if ($this->serialize) {
$cachedData = unserialize($cachedData);
}
unset($this->cached[$key]);
$response = new Response($cachedData['c'], $cachedData['h'], $cachedData['b']);
$response->setHeader('Age', time() - strtotime($response->getDate() ?: 'now'));
if (!$response->hasHeader('X-Guzzle-Cache')) {
$response->setHeader('X-Guzzle-Cache', "key={$key}");
}
// Validate that the response satisfies the request
if ($this->canResponseSatisfyRequest($request, $response)) {
$request->setResponse($response);
}
}
}
示例6: handle304Response
/**
* Handle a 304 response and ensure that it is still valid
*
* @param RequestInterface $request Request that was sent
* @param Response $validateResponse Response received
* @param Response $response Original cached response
*
* @return bool Returns true if valid, false if invalid
*/
protected function handle304Response(RequestInterface $request, Response $validateResponse, Response $response)
{
static $replaceHeaders = array('Date', 'Expires', 'Cache-Control', 'ETag', 'Last-Modified');
// Make sure that this response has the same ETag
if ($validateResponse->getEtag() != $response->getEtag()) {
return false;
}
// Replace cached headers with any of these headers from the
// origin server that might be more up to date
$modified = false;
foreach ($replaceHeaders as $name) {
if ($validateResponse->hasHeader($name)) {
$modified = true;
$response->setHeader($name, $validateResponse->getHeader($name));
}
}
// Store the updated response in cache
if ($modified && $this->canCache->canCacheResponse($response)) {
$this->storage->cache($request, $response);
}
return true;
}
示例7: addResponseHeaders
/**
* Add the plugin's headers to a response
*
* @param string $cacheKey Cache key
* @param RequestInterface $request Request
* @param Response $response Response to add headers to
*/
protected function addResponseHeaders($cacheKey, RequestInterface $request, Response $response)
{
if (!$response->hasHeader('X-Guzzle-Cache')) {
$response->setHeader('X-Guzzle-Cache', "key={$cacheKey}");
}
$response->addHeader('Via', sprintf('%s GuzzleCache/%s', $request->getProtocolVersion(), Version::VERSION));
if ($this->debugHeaders) {
if ($request->getParams()->get('cache.lookup') === true) {
$response->addHeader('X-Cache-Lookup', 'HIT from GuzzleCache');
} else {
$response->addHeader('X-Cache-Lookup', 'MISS from GuzzleCache');
}
if ($request->getParams()->get('cache.hit') === true) {
$response->addHeader('X-Cache', 'HIT from GuzzleCache');
} elseif ($request->getParams()->get('cache.hit') === 'error') {
$response->addHeader('X-Cache', 'HIT_ERROR from GuzzleCache');
} else {
$response->addHeader('X-Cache', 'MISS from GuzzleCache');
}
}
if ($response->isFresh() === false) {
$response->addHeader('Warning', sprintf('110 GuzzleCache/%s "Response is stale"', Version::VERSION));
if ($request->getParams()->get('cache.hit') === 'error') {
$response->addHeader('Warning', sprintf('111 GuzzleCache/%s "Revalidation failed"', Version::VERSION));
}
}
}
示例8: parseResponse
private function parseResponse(Response $response, $path)
{
$statusCode = $response->getStatusCode();
if ($statusCode !== 200) {
throw new UnexpectedValueException(sprintf('Expected status code 200 from "%s", got %d', $path, $statusCode));
}
$contentType = $response->hasHeader('content-type') ? $response->getContentType() : '';
if (substr($contentType, 0, 10) !== 'text/plain') {
throw new UnexpectedValueException(sprintf('Expected content type "text/plain" from "%s", got "%s"', $path, $contentType));
}
return $this->parseRequestFromResponse($response, $path);
}
示例9: hasHeader
/**
* {@inheritdoc}
*/
public function hasHeader($header)
{
return $this->response->hasHeader($header);
}
示例10: onRequestBeforeSend
/**
* Check if a response in cache will satisfy the request before sending
*
* @param Event $event
*/
public function onRequestBeforeSend(Event $event)
{
$request = $event['request'];
// Only cache cacheable requests
if ($cacheFilter = $request->getParams()->get('cache.filter_strategy')) {
if (!call_user_func($cacheFilter, $request)) {
return;
}
} elseif (!$request->canCache()) {
return;
}
$hashKey = $this->getCacheKey($request);
$this->cached[$request] = $hashKey;
$cachedData = $this->adapter->fetch($hashKey);
// If the cached data was found, then make the request into a
// manually set request
if ($cachedData) {
unset($this->cached[$request]);
$response = new Response($cachedData[0], $cachedData[1], $cachedData[2]);
$response->setHeader('Age', time() - strtotime($response->getDate() ?: 'now'));
if (!$response->hasHeader('X-Guzzle-Cache')) {
$response->setHeader('X-Guzzle-Cache', "key={$hashKey}");
}
// Validate that the response satisfies the request
if ($this->canResponseSatisfyRequest($request, $response)) {
$request->setResponse($response);
}
}
}