本文整理汇总了PHP中Psr\Http\Message\ResponseInterface::getHeaderLine方法的典型用法代码示例。如果您正苦于以下问题:PHP ResponseInterface::getHeaderLine方法的具体用法?PHP ResponseInterface::getHeaderLine怎么用?PHP ResponseInterface::getHeaderLine使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Psr\Http\Message\ResponseInterface
的用法示例。
在下文中一共展示了ResponseInterface::getHeaderLine方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getHeaderLine
/**
* {@inheritDoc}
*/
public function getHeaderLine($name, $default = null)
{
if (!$this->decoratedResponse->hasHeader($name)) {
return $default;
}
return $this->decoratedResponse->getHeaderLine($name);
}
示例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")) {
$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'));
}
示例3: deserialize
/**
* @param ResponseInterface $response
* @param string $class
*
* @return array
*/
public function deserialize(ResponseInterface $response, $class)
{
$body = $response->getBody()->__toString();
if (strpos($response->getHeaderLine('Content-Type'), 'application/json') !== 0) {
throw new DeserializeException('The ArrayDeserializer cannot deserialize response with Content-Type:' . $response->getHeaderLine('Content-Type'));
}
$content = json_decode($body, true);
if (JSON_ERROR_NONE !== json_last_error()) {
throw new DeserializeException(sprintf('Error (%d) when trying to json_decode response', json_last_error()));
}
return $content;
}
示例4: __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;
}
示例5: 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'));
}
示例6: modify
public function modify(ResponseInterface $response) : ResponseInterface
{
if (false === strpos($response->getHeaderLine('Content-Type'), 'application/json')) {
return $response;
}
return $response->withHeader('Content-Type', $this->contentType);
}
示例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: sendBody
/**
* Send body as response.
*
* @param ResponseInterface $response Response object.
*
* @return void
*/
public function sendBody(ResponseInterface $response)
{
$range = $this->getContentRange($response->getHeaderLine('content-range'));
$maxBufferLength = $this->getMaxBufferLength() ?: 8192;
if ($range === false) {
$body = $response->getBody();
$body->rewind();
while (!$body->eof()) {
echo $body->read($maxBufferLength);
$this->delay();
}
return;
}
list($unit, $first, $last, $length) = array_values($range);
++$last;
$body = $response->getBody();
$body->seek($first);
$position = $first;
while (!$body->eof() && $position < $last) {
// The latest part
if ($position + $maxBufferLength > $last) {
echo $body->read($last - $position);
$this->delay();
break;
}
echo $body->read($maxBufferLength);
$position = $body->tell();
$this->delay();
}
}
示例9: jsonFromResponse
/**
* Decodes a JSON response.
*
* @param ResponseInterface $response
* @param bool $assoc [optional] Return an associative array?
* @return mixed
*/
static function jsonFromResponse(ResponseInterface $response, $assoc = false)
{
if ($response->getHeaderLine('Content-Type') != 'application/json') {
throw new \RuntimeException("HTTP response is not of type JSON");
}
return json_decode($response->getBody(), $assoc);
}
示例10: extractHeader
/**
* Extract a single header from the response into the result.
*/
private function extractHeader($name, Shape $shape, ResponseInterface $response, &$result)
{
$value = $response->getHeaderLine($shape['locationName'] ?: $name);
switch ($shape->getType()) {
case 'float':
case 'double':
$value = (double) $value;
break;
case 'long':
$value = (int) $value;
break;
case 'boolean':
$value = filter_var($value, FILTER_VALIDATE_BOOLEAN);
break;
case 'blob':
$value = base64_decode($value);
break;
case 'timestamp':
try {
$value = new DateTimeResult($value);
break;
} catch (\Exception $e) {
// If the value cannot be parsed, then do not add it to the
// output structure.
return;
}
}
$result[$name] = $value;
}
示例11: 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'));
}
示例12: __invoke
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
{
if (false === strpos($response->getHeaderLine('Content-Type'), 'application/json')) {
return $next($request, $response);
}
return $next($request, $response->withHeader('Content-Type', $this->contentType));
}
示例13: supportPagination
/** {@inheritdoc} */
public function supportPagination(array $data, ResponseInterface $response, ResponseDefinition $responseDefinition)
{
$support = true;
foreach ($this->paginationHeaders as $headerName) {
$support = $support & $response->getHeaderLine($headerName) !== '';
}
return (bool) $support;
}
示例14: extractData
/**
* @return array
*/
public function extractData()
{
if (false !== $this->data) {
return $this->data;
}
$stream = $this->response->getBody();
if ($stream->tell()) {
$stream->rewind();
}
$body = $stream->getContents();
$contentType = $this->response->getHeaderLine('Content-Type');
$contentTypeParts = explode(';', $contentType);
$mimeType = trim(reset($contentTypeParts));
$data = static::parseStringByFormat($body, $mimeType);
$this->data = $data;
return $data;
}
示例15: getExpiration
/**
* Check the cache headers and return the expiration time.
*
* @param ResponseInterface $response
*
* @return Datetime|null
*/
private static function getExpiration(ResponseInterface $response)
{
//Cache-Control
$cacheControl = $response->getHeaderLine('Cache-Control');
if (!empty($cacheControl)) {
$cacheControl = self::parseCacheControl($cacheControl);
//Max age
if (isset($cacheControl['max-age'])) {
return new Datetime('@' . (time() + (int) $cacheControl['max-age']));
}
}
//Expires
$expires = $response->getHeaderLine('Expires');
if (!empty($expires)) {
return new Datetime($expires);
}
}