本文整理汇总了PHP中GuzzleHttp\Psr7\Response::getHeader方法的典型用法代码示例。如果您正苦于以下问题:PHP Response::getHeader方法的具体用法?PHP Response::getHeader怎么用?PHP Response::getHeader使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类GuzzleHttp\Psr7\Response
的用法示例。
在下文中一共展示了Response::getHeader方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __construct
public function __construct(\GuzzleHttp\Psr7\Response $response)
{
$json = false;
$data = $response->getBody();
$this->rawData = $data;
$this->response = $response;
if ($response->hasHeader('Content-Type')) {
// Let's see if it is JSON
$contentType = $response->getHeader('Content-Type');
if (strstr($contentType[0], 'json')) {
$json = true;
$data = json_decode($data);
}
}
if (!$json) {
// We can do another test here
$decoded = json_decode($response->getBody());
if ($decoded) {
$json = true;
$data = $decoded;
}
}
$this->setData($data);
$this->setIsJson($json);
}
示例2: testCanConstructWithHeadersAsArray
public function testCanConstructWithHeadersAsArray()
{
$r = new Response(200, ['Foo' => ['baz', 'bar']]);
$this->assertSame(['Foo' => ['baz', 'bar']], $r->getHeaders());
$this->assertSame('baz, bar', $r->getHeaderLine('Foo'));
$this->assertSame(['baz', 'bar'], $r->getHeader('Foo'));
}
示例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: __construct
/**
* Response constructor
*
* @param HttpResponse $httpResponse
* @return self
*/
public function __construct(HttpResponse $httpResponse)
{
$header = $httpResponse->getHeader('Content-Type');
if (!is_array($header) && stripos($header, 'text/javascript') !== false) {
$this->_response = $this->processJson($httpResponse->getBody());
} else {
$this->_response = $this->processXml($httpResponse->getBody());
}
}
示例5: getCacheTtl
/**
* Get cache ttl value
*
* @param Response $response
*
* @return int
*/
protected function getCacheTtl(Response $response)
{
if ($this->useHeaderTtl && $response->hasHeader('Cache-Control')) {
$cacheControl = $response->getHeader('Cache-Control')[0];
if (preg_match('`max-age=(\\d+)`', $cacheControl, $match)) {
return intval($match[1]);
}
}
return $this->defaultTtl;
}
示例6: create
/**
* @param ObjectInterface $object
* @param array $data
* @param Response $response
* @return ObjectInterface
*/
public function create(ObjectInterface $object, array $data, Response $response = null)
{
// If object had relationId, we pass it to new object
if ($object->getAttribute("relationId")) {
$relationId = $object->getAttribute("relationId");
$object = new $object($relationId);
} else {
$object = new $object();
}
if (isset($data['items']) or isset($data[0])) {
// If we have list of items
$object = $this->createList($object, $data);
} else {
if (isset($data["id"])) {
// TODO: Some things don't provide ID, possible BUG, that it isn't implemented in CREST
$object->setAttribute("id", $data['id']);
$uri = $object->getAttribute("uri") . $object->getAttribute("id") . "/";
$object->setAttribute("uri", $uri);
} elseif (!empty($data['href']) && is_numeric(basename($data["href"]))) {
// TODO: Meybe not bug, so we just take it from href
$object->setAttribute("id", (int) basename($data["href"]));
$uri = $object->getAttribute("uri") . $object->getAttribute("id") . "/";
$object->setAttribute("uri", $uri);
}
foreach ($data as $key => $value) {
if (array_key_exists($key, $object->getRelations())) {
// ˇˇ
if (is_string($value)) {
// TODO: Sometimes relationship is only a string with href, not array like usually.
// TODO: When crest behaves as it should, remove this!
// Seen in GET crest.../wars/21/ value "killmails"
$object->setValue($key, $value);
continue;
}
// ^^
// Create new relation object with relation_id
$relation = $object->getRelations($key);
$object->setValue($key, $this->create(new $relation($object->getAttribute("id")), $value));
} else {
$object->setValue($key, $value);
}
}
}
// Add cache-control header to Object, but only if this Endpoint made request
// we cant add cache-control for relationships, as we don't have data for them
// Add HTTP Code, might be useful
if ($response) {
// On POST/PUT/DELETE responses cache-control isn't provided.
if ($response->hasHeader("cache-control")) {
$object->setAttribute("cache", $response->getHeader("cache-control")[0]);
}
$object->setAttribute("httpCode", $response->getStatusCode());
}
return $object;
}
示例7: parse
public function parse(Response $response)
{
$collection = new Collection();
if (!$response->getBody()) {
return $collection;
}
// help bad responses be more multipart compliant
$body = "\r\n" . $response->getBody()->__toString() . "\r\n";
// multipart
preg_match('/boundary\\=\\"(.*?)\\"/', $response->getHeader('Content-Type'), $matches);
if (isset($matches[1])) {
$boundary = $matches[1];
} else {
preg_match('/boundary\\=(.*?)(\\s|$|\\;)/', $response->getHeader('Content-Type'), $matches);
$boundary = $matches[1];
}
// strip quotes off of the boundary
$boundary = preg_replace('/^\\"(.*?)\\"$/', '\\1', $boundary);
// clean up the body to remove a reamble and epilogue
$body = preg_replace('/^(.*?)\\r\\n--' . $boundary . '\\r\\n/', "\r\n--{$boundary}\r\n", $body);
// make the last one look like the rest for easier parsing
$body = preg_replace('/\\r\\n--' . $boundary . '--/', "\r\n--{$boundary}\r\n", $body);
// cut up the message
$multi_parts = explode("\r\n--{$boundary}\r\n", $body);
// take off anything that happens before the first boundary (the preamble)
array_shift($multi_parts);
// take off anything after the last boundary (the epilogue)
array_pop($multi_parts);
$message_parser = new MessageParser();
$parser = new Single();
// go through each part of the multipart message
foreach ($multi_parts as $part) {
// get Guzzle to parse this multipart section as if it's a whole HTTP message
$parts = $message_parser->parseResponse("HTTP/1.1 200 OK\r\n" . $part);
// now throw this single faked message through the Single GetObject response parser
$single = new Response($parts['code'], $parts['headers'], Stream::factory($parts['body']));
$obj = $parser->parse($single);
// add information about this multipart to the returned collection
$collection->push($obj);
}
return $collection;
}
示例8: testBase
public function testBase()
{
$response = new Response(200, ['Cache-Control' => ['max-age = 120', ' stale-while-revalidate= 60 ', ' private ', 'zero=0', 'nothing = ', 'false = false']]);
$values = new KeyValueHttpHeader($response->getHeader('Cache-Control'));
$this->assertTrue($values->has('max-age'));
$this->assertTrue($values->has('stale-while-revalidate'));
$this->assertTrue($values->has('private'));
$this->assertTrue($values->has('zero'));
$this->assertTrue($values->has('nothing'));
$this->assertTrue($values->has('false'));
$this->assertEquals(120, $values->get('max-age'));
$this->assertEquals(60, $values->get('stale-while-revalidate'));
$this->assertEquals(0, $values->get('zero'));
$this->assertEquals('', $values->get('nothing'));
$this->assertEquals('false', $values->get('false'));
}
示例9: verifySubProtocol
public function verifySubProtocol(Request $request, Response $response)
{
$subProtocolRequest = $request->getHeader('Sec-WebSocket-Protocol');
if (empty($subProtocolRequest)) {
return true;
}
$subProtocolResponse = $response->getHeader('Sec-WebSocket-Protocol');
if (count($subProtocolResponse) !== 1) {
// there should be exactly one subprotocol sent back if we requested
return false;
}
if (in_array($subProtocolResponse[0], $subProtocolRequest)) {
// the response is one of the requested subprotocols
return true;
}
return false;
}
示例10: validateResponse
/**
* Validate the HTTP response and throw exceptions on errors
*
* @param HttpResponse $response
* @throws ServerException
*/
private function validateResponse($response)
{
if ($response->getStatusCode() !== 200) {
$statusCode = $response->getStatusCode();
throw new ServerException('Server responded with HTTP status ' . $statusCode, $statusCode);
} else {
if (strpos($response->getHeader('Content-Type')[0], 'application/json') === false) {
throw new ServerException('Server did not respond with the expected content-type (application/json)');
}
}
try {
$body = (string) $response->getBody();
$json = json_decode($body, true);
if (json_last_error()) {
throw new ServerException(json_last_error_msg());
}
} catch (RuntimeException $e) {
throw new ServerException($e->getMessage());
}
}
示例11: testCanSetHeaderAsArray
public function testCanSetHeaderAsArray()
{
$r = new Response(200, ['foo' => ['baz ', ' bar ']]);
$this->assertEquals('baz, bar', $r->getHeaderLine('foo'));
$this->assertEquals(['baz', 'bar'], $r->getHeader('foo'));
}
示例12: handleResponse
/**
* @param RequestInterface $request
* @param Response $response
* @return \Guzzle\Http\EntityBodyInterface|mixed|string
*/
public function handleResponse(Request $request, Response $response)
{
$body = $response->getBody(true);
$contentType = $response->getHeader('Content-Type')[0];
switch ($contentType) {
case "application/json":
$body = json_decode($body, true);
break;
}
return [$response->getStatusCode(), $body];
}
示例13: buildFailureMessage
/**
* Builds a pretty failure message from the specified response's warning headers, using the specified request for
* additional information.
* @param \GuzzleHttp\Psr7\Request $request HTTP request object
* @param \GuzzleHttp\Psr7\Response $response HTTP response object
* @return string
*/
protected function buildFailureMessage(Request $request, Response $response)
{
$message = "Unsuccessful " . $request->getMethod() . " to " . $request->getUri() . " (" . $response->getStatusCode() . ")";
$warnings = $response->getHeader('Warning');
foreach ($warnings as $w) {
$message .= "\nWarning: " . $w;
}
return $message;
}
示例14: type
/**
* Response contents type.
*
* @return string The response header content type.
*/
protected function type()
{
$type = $this->response->getHeader('Content-Type');
return strtolower(reset($type));
}
示例15: _updateRateLimits
/**
* After each request update the rate limits that are received in the headers
*/
private function _updateRateLimits()
{
App::getInstance()->getRateLimit()->update(current($this->_response->getHeader(ApiHelper::HEADER_RATE_LIMIT)), current($this->_response->getHeader(ApiHelper::HEADER_RATE_LIMIT_REMAINING)), current($this->_response->getHeader(ApiHelper::HEADER_RATE_LIMIT_RESET)));
}