本文整理汇总了PHP中Psr\Http\Message\ResponseInterface::hasHeader方法的典型用法代码示例。如果您正苦于以下问题:PHP ResponseInterface::hasHeader方法的具体用法?PHP ResponseInterface::hasHeader怎么用?PHP ResponseInterface::hasHeader使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Psr\Http\Message\ResponseInterface
的用法示例。
在下文中一共展示了ResponseInterface::hasHeader方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: setHeadersFromArray
/**
* Add headers represented by an array of header lines.
*
* @param string[] $headers Response headers as array of header lines.
*
* @return $this
*
* @throws \UnexpectedValueException For invalid header values.
* @throws \InvalidArgumentException For invalid status code arguments.
*/
public function setHeadersFromArray(array $headers)
{
$statusLine = trim(array_shift($headers));
$parts = explode(' ', $statusLine, 3);
if (count($parts) < 2 || substr(strtolower($parts[0]), 0, 5) !== 'http/') {
throw new \UnexpectedValueException(sprintf('"%s" is not a valid HTTP status line', $statusLine));
}
$reasonPhrase = count($parts) > 2 ? $parts[2] : '';
$this->response = $this->response->withStatus((int) $parts[1], $reasonPhrase)->withProtocolVersion(substr($parts[0], 5));
foreach ($headers as $headerLine) {
$headerLine = trim($headerLine);
if ('' === $headerLine) {
continue;
}
$parts = explode(':', $headerLine, 2);
if (count($parts) !== 2) {
throw new \UnexpectedValueException(sprintf('"%s" is not a valid HTTP header line', $headerLine));
}
$name = trim(urldecode($parts[0]));
$value = trim(urldecode($parts[1]));
if ($this->response->hasHeader($name)) {
$this->response = $this->response->withAddedHeader($name, $value);
} else {
$this->response = $this->response->withHeader($name, $value);
}
}
return $this;
}
示例2: getCacheObject
/**
* @param ResponseInterface $response
* @return CacheEntry|null entry to save, null if can't cache it
*/
protected function getCacheObject(ResponseInterface $response)
{
if ($response->hasHeader("Cache-Control")) {
$values = new KeyValueHttpHeader($response->getHeader("Cache-Control"));
if ($values->has('no-store')) {
// No store allowed (maybe some sensitives data...)
return null;
}
if ($values->has('no-cache')) {
// Stale response see RFC7234 section 5.2.1.4
$entry = new CacheEntry($response, new \DateTime('-1 seconds'));
return $entry->hasValidationInformation() ? $entry : null;
}
if ($values->has('max-age')) {
return new CacheEntry($response, new \DateTime('+' . $values->get('max-age') . 'seconds'));
}
return new CacheEntry($response, new \DateTime());
}
if ($response->hasHeader("Expires")) {
$expireAt = \DateTime::createFromFormat('D, d M Y H:i:s T', $response->getHeaderLine("Expires"));
if ($expireAt !== FALSE) {
return new CacheEntry($response, $expireAt);
}
}
return new CacheEntry($response, new \DateTime('-1 seconds'));
}
示例3: getHeaderLine
/**
* {@inheritDoc}
*/
public function getHeaderLine($name, $default = null)
{
if (!$this->decoratedResponse->hasHeader($name)) {
return $default;
}
return $this->decoratedResponse->getHeaderLine($name);
}
示例4: getCacheObject
/**
* @param ResponseInterface $response
* @return CacheEntry|null entry to save, null if can't cache it
*/
protected function getCacheObject(ResponseInterface $response)
{
if ($response->hasHeader("Cache-Control")) {
$cacheControlDirectives = $response->getHeader("Cache-Control");
if (in_array("no-store", $cacheControlDirectives)) {
// No store allowed (maybe some sensitives data...)
return null;
}
if (in_array("no-cache", $cacheControlDirectives)) {
// Stale response see RFC7234 section 5.2.1.4
$entry = new CacheEntry($response, new \DateTime('-1 seconds'));
return $entry->hasValidationInformation() ? $entry : null;
}
$matches = [];
if (preg_match('/^max-age=([0-9]*)$/', $response->getHeaderLine("Cache-Control"), $matches)) {
// Handle max-age header
return new CacheEntry($response, new \DateTime('+' . $matches[1] . 'seconds'));
}
}
if ($response->hasHeader("Expires")) {
$expireAt = \DateTime::createFromFormat('D, d M Y H:i:s T', $response->getHeaderLine("Expires"));
if ($expireAt !== FALSE) {
return new CacheEntry($response, $expireAt);
}
}
return new CacheEntry($response, new \DateTime('1 days ago'));
}
示例5: __construct
/**
* @param string $message
* @param ResponseInterface $response
* @param \DateTime $responseDateTime
*/
public function __construct($message, ResponseInterface $response, \DateTime $responseDateTime)
{
parent::__construct($message, intval($response->getStatusCode()));
$this->response = $response;
$this->responseDateTime = $responseDateTime;
$this->retrySeconds = $response->hasHeader('Retry-After') ? intval($response->getHeaderLine('Retry-After')) : null;
}
示例6: getCacheObject
/**
* @param RequestInterface $request
* @param ResponseInterface $response
* @return CacheEntry|null entry to save, null if can't cache it
*/
protected function getCacheObject(RequestInterface $request, ResponseInterface $response)
{
if (!isset($this->statusAccepted[$response->getStatusCode()])) {
// Don't cache it
return;
}
$cacheControl = new KeyValueHttpHeader($response->getHeader('Cache-Control'));
$varyHeader = new KeyValueHttpHeader($response->getHeader('Vary'));
if ($varyHeader->has('*')) {
// This will never match with a request
return;
}
if ($cacheControl->has('no-store')) {
// No store allowed (maybe some sensitives data...)
return;
}
if ($cacheControl->has('no-cache')) {
// Stale response see RFC7234 section 5.2.1.4
$entry = new CacheEntry($request, $response, new \DateTime('-1 seconds'));
return $entry->hasValidationInformation() ? $entry : null;
}
foreach ($this->ageKey as $key) {
if ($cacheControl->has($key)) {
return new CacheEntry($request, $response, new \DateTime('+' . (int) $cacheControl->get($key) . 'seconds'));
}
}
if ($response->hasHeader('Expires')) {
$expireAt = \DateTime::createFromFormat(\DateTime::RFC1123, $response->getHeaderLine('Expires'));
if ($expireAt !== false) {
return new CacheEntry($request, $response, $expireAt);
}
}
return new CacheEntry($request, $response, new \DateTime('-1 seconds'));
}
示例7: __construct
/**
* WosObjectId constructor.
*
* @param ResponseInterface $httpResponse
*/
public function __construct(ResponseInterface $httpResponse)
{
if (!$httpResponse->hasHeader('x-ddn-oid')) {
throw new InvalidResponseException('x-ddn-oid', 'reserve object');
}
$this->objectId = $httpResponse->getHeaderLine('x-ddn-oid');
}
示例8: decode
public static function decode(ResponseInterface $response)
{
if ($response->hasHeader('Content-Type') && $response->getHeader('Content-Type')[0] == 'application/json') {
return json_decode((string) $response->getBody(), true);
}
return (string) $response->getBody();
}
示例9: createStream
/**
* Create the stream.
*
* @param $socket
* @param ResponseInterface $response
*
* @return Stream
*/
protected function createStream($socket, ResponseInterface $response)
{
$size = null;
if ($response->hasHeader('Content-Length')) {
$size = (int) $response->getHeaderLine('Content-Length');
}
return new Stream($socket, $size);
}
示例10: __construct
/**
* WosObject constructor.
*
* @param ResponseInterface $httpResponse
*/
public function __construct(ResponseInterface $httpResponse)
{
if (!$httpResponse->hasHeader('x-ddn-oid')) {
throw new InvalidResponseException('x-ddn-oid', 'get object');
}
$this->responseData = $httpResponse->getBody();
$this->metadata = new WosObjectMetadata($httpResponse);
$this->objectId = new WosObjectId($httpResponse);
}
示例11: getPagination
/** {@inheritdoc} */
public function getPagination(array $data, ResponseInterface $response, ResponseDefinition $responseDefinition)
{
$paginationLinks = null;
if ($response->hasHeader('Link')) {
$links = self::parseHeaderLinks($response->getHeader('Link'));
$paginationLinks = new PaginationLinks($links['first'], $links['last'], $links['next'], $links['prev']);
}
return new Pagination((int) $response->getHeaderLine($this->paginationHeaders['page']), (int) $response->getHeaderLine($this->paginationHeaders['perPage']), (int) $response->getHeaderLine($this->paginationHeaders['totalItems']), (int) $response->getHeaderLine($this->paginationHeaders['totalPages']), $paginationLinks);
}
示例12: checkRedirect
/**
* @param RequestInterface $request
* @param array $options
* @param ResponseInterface|PromiseInterface $response
*
* @return ResponseInterface|PromiseInterface
*/
public function checkRedirect(RequestInterface $request, array $options, ResponseInterface $response)
{
if (substr($response->getStatusCode(), 0, 1) != '3' || !$response->hasHeader('Location')) {
return $response;
}
$this->guardMax($request, $options);
$nextRequest = $this->modifyRequest($request, $options, $response);
return $this($nextRequest, $options);
}
示例13: emit
/**
* @param Psr7Response
* @param ReactResponse
* @return void
*/
private function emit(Psr7Response $psr7Response, ReactResponse $reactResponse)
{
if (!$psr7Response->hasHeader('Content-Type')) {
$psr7Response = $psr7Response->withHeader('Content-Type', 'text/html');
}
$reactResponse->writeHead($psr7Response->getStatusCode(), $psr7Response->getHeaders());
$body = $psr7Response->getBody();
$body->rewind();
$reactResponse->end($body->getContents());
$body->close();
}
示例14: getResponseAsJson
/**
* Returns the json object if the response contains valid json, otherwise null.
*
* @param ResponseInterface $response
* @return mixed|null
*/
protected function getResponseAsJson(ResponseInterface $response)
{
if ($response->hasHeader('Content-Type')) {
$typeHeader = $response->getHeader('Content-Type');
$contentType = array_shift($typeHeader);
if (stripos($contentType, 'application/json') === 0) {
return json_decode($response->getBody(), false);
}
}
return null;
}
示例15: injectContentLength
/**
* Inject the Content-Length header if is not already present.
*
* @param ResponseInterface $response
* @return ResponseInterface
*/
private function injectContentLength(ResponseInterface $response)
{
if (!$response->hasHeader('Content-Length')) {
// PSR-7 indicates int OR null for the stream size; for null values,
// we will not auto-inject the Content-Length.
if (null !== $response->getBody()->getSize()) {
return $response->withHeader('Content-Length', (string) $response->getBody()->getSize());
}
}
return $response;
}