本文整理汇总了PHP中Psr\Http\Message\ResponseInterface::getHeader方法的典型用法代码示例。如果您正苦于以下问题:PHP ResponseInterface::getHeader方法的具体用法?PHP ResponseInterface::getHeader怎么用?PHP ResponseInterface::getHeader使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Psr\Http\Message\ResponseInterface
的用法示例。
在下文中一共展示了ResponseInterface::getHeader方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __construct
/**
* @param ResponseInterface $response
* @param \DateTime $staleAt
* @param \DateTime|null $staleIfErrorTo if null, detected with the headers (RFC 5861)
* @param \DateTime $staleWhileRevalidateTo
*/
public function __construct(ResponseInterface $response, \DateTime $staleAt, \DateTime $staleIfErrorTo = null, \DateTime $staleWhileRevalidateTo = null)
{
$this->response = $response;
$this->staleAt = $staleAt;
if ($staleIfErrorTo === null) {
$headersCacheControl = $response->getHeader("Cache-Control");
if (!in_array("must-revalidate", $headersCacheControl)) {
foreach ($headersCacheControl as $directive) {
$matches = [];
if (preg_match('/^stale-if-error=([0-9]*)$/', $directive, $matches)) {
$this->staleIfErrorTo = new \DateTime('+' . $matches[1] . 'seconds');
break;
}
}
}
} else {
$this->staleIfErrorTo = $staleIfErrorTo;
}
if ($staleWhileRevalidateTo === null) {
foreach ($response->getHeader("Cache-Control") as $directive) {
$matches = [];
if (preg_match('/^stale-while-revalidate=([0-9]*)$/', $directive, $matches)) {
$this->staleWhileRevalidateTo = new \DateTime('+' . $matches[1] . 'seconds');
break;
}
}
} else {
$this->staleWhileRevalidateTo = $staleWhileRevalidateTo;
}
}
示例2: theResponseHeadersShouldContain
/**
* @When the response headers should contain :expectedHeader
*/
public function theResponseHeadersShouldContain($expectedHeader)
{
list($headerTitle, $expectedHeaderValue) = explode(':', $expectedHeader);
$expectedHeaderValue = trim($expectedHeaderValue);
$headerValuesReceived = $this->responseReceived->getHeader($headerTitle);
\PHPUnit_Framework_Assert::assertEquals($expectedHeaderValue, $headerValuesReceived[0]);
}
示例3: 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'));
}
示例4: _getResult
/**
* @param ResponseInterface $response
*
* @return ApiResult
*/
protected function _getResult($response)
{
if (!$response instanceof ResponseInterface) {
throw new \InvalidArgumentException("{$response} should be an instance of ResponseInterface");
}
$result = new ApiResult();
$result->setStatusCode($response->getStatusCode());
$callId = $response->getHeader('X-Call-Id');
if (!empty($callId)) {
$result->setCallId($callId);
}
$decoded = json_decode((string) $response->getBody());
if (isset($decoded->meta) && isset($decoded->data) && isset($decoded->meta->code) && $decoded->meta->code == $response->getStatusCode()) {
$meta = $decoded->meta;
$data = $decoded->data;
if (isset($meta->message)) {
$result->setStatusMessage($meta->message);
}
$result->setContent(json_encode($data));
} else {
$result->setContent((string) $response->getBody());
}
$result->setHeaders($response->getHeaders());
return $result;
}
示例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")) {
$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'));
}
示例6: checkResponse
protected function checkResponse(ResponseInterface $response, $data)
{
// Metadata info
$contentTypeRaw = $response->getHeader('Content-Type');
$contentTypeArray = explode(';', reset($contentTypeRaw));
$contentType = reset($contentTypeArray);
// Response info
$responseCode = $response->getStatusCode();
$responseMessage = $response->getReasonPhrase();
// Data info
$error = !empty($data['error']) ? $data['error'] : null;
$errorCode = !empty($error['error_code']) ? $error['error_code'] : $responseCode;
$errorDescription = !empty($data['error_description']) ? $data['error_description'] : null;
$errorMessage = !empty($error['error_msg']) ? $error['error_msg'] : $errorDescription;
$message = $errorMessage ?: $responseMessage;
// Request/meta validation
if (399 < $responseCode) {
throw new IdentityProviderException($message, $responseCode, $data);
}
// Content validation
if ('application/json' != $contentType) {
throw new IdentityProviderException($message, $responseCode, $data);
}
if ($error) {
throw new IdentityProviderException($errorMessage, $errorCode, $data);
}
}
示例7: 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();
}
示例8: 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'));
}
示例9: handleInvalidContentType
private function handleInvalidContentType(HalClientInterface $client, RequestInterface $request, ResponseInterface $response, $ignoreInvalidContentType)
{
if ($ignoreInvalidContentType) {
return new HalResource($client);
}
$types = $response->getHeader('Content-Type') ?: ['none'];
throw new Exception\BadResponseException(sprintf('Request did not return a valid content type. Returned content type: %s.', implode(', ', $types)), $request, $response, new HalResource($client));
}
示例10: Resource
function it_returns_a_processed_resource(ResponseInterface $response, HttpClient $httpClient, Processor $processor)
{
$response->getHeader('content-type')->willReturn(['application/hal+json']);
$resource = new Resource([], []);
$httpClient->get('http://api.test.com/')->willReturn($response);
$processor->process($response, $this)->willReturn($resource);
$this->get('http://api.test.com/')->shouldReturn($resource);
}
示例11: getCacheObject
/**
* {@inheritdoc}
*/
protected function getCacheObject(RequestInterface $request, ResponseInterface $response)
{
$cacheControl = new KeyValueHttpHeader($response->getHeader('Cache-Control'));
if ($cacheControl->has('private')) {
return;
}
return parent::getCacheObject($request, $response);
}
示例12: 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);
}
示例13: __construct
/**
* Constructor
*
* @param \Psr\Http\Message\ResponseInterface $response
* @param string $userAgent
* @throws XRobotsTagParser\Exceptions\XRobotsTagParserException
*/
public function __construct(ResponseInterface $response, $userAgent = '')
{
parent::__construct($userAgent);
$headers = [];
foreach ($response->getHeader(parent::HEADER_RULE_IDENTIFIER) as $name => $values) {
$headers[] = $name . ': ' . implode(' ', $values) . "\r\n";
}
$this->parse($headers);
}
示例14: getOrchestrateRequestId
public function getOrchestrateRequestId()
{
$this->settlePromise();
if ($this->_response) {
$value = $this->_response->getHeader('X-ORCHESTRATE-REQ-ID');
return isset($value[0]) ? $value[0] : '';
}
return '';
}
示例15: create
/**
*
* @param RequestInterface $request
* @param ResponseInterface $response
* @param \Exception $previous
* @param array $ctx
* @return BearerErrorResponseException
*/
public static function create(RequestInterface $request, ResponseInterface $response = null, \Exception $previous = null, array $ctx = [])
{
unset($previous, $ctx);
$label = 'Bearer error response';
$bearerReason = self::headerToReason($response->getHeader('WWW-Authenticate'));
$message = $label . PHP_EOL . implode(PHP_EOL, ['[status code] ' . $response->getStatusCode(), '[reason phrase] ' . $response->getReasonPhrase(), '[bearer reason] ' . $bearerReason, '[url] ' . $request->getUri()]);
$exception = new static($message, $request, $response);
$exception->setBearerReason($bearerReason);
return $exception;
}