本文整理汇总了PHP中GuzzleHttp\Message\Response::getHeader方法的典型用法代码示例。如果您正苦于以下问题:PHP Response::getHeader方法的具体用法?PHP Response::getHeader怎么用?PHP Response::getHeader使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类GuzzleHttp\Message\Response
的用法示例。
在下文中一共展示了Response::getHeader方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: createResponse
/**
* Taken from Mink\BrowserKitDriver
*
* @param Response $response
*
* @return \Symfony\Component\BrowserKit\Response
*/
protected function createResponse(Response $response)
{
$contentType = $response->getHeader('Content-Type');
$matches = null;
if (!$contentType or strpos($contentType, 'charset=') === false) {
$body = $response->getBody(true);
if (preg_match('/\\<meta[^\\>]+charset *= *["\']?([a-zA-Z\\-0-9]+)/i', $body, $matches)) {
$contentType .= ';charset=' . $matches[1];
}
$response->setHeader('Content-Type', $contentType);
}
$headers = $response->getHeaders();
$status = $response->getStatusCode();
$matchesMeta = null;
$matchesHeader = null;
$isMetaMatch = preg_match('/\\<meta[^\\>]+http-equiv="refresh" content="(\\d*)\\s*;?\\s*url=(.*?)"/i', $response->getBody(true), $matchesMeta);
$isHeaderMatch = preg_match('~(\\d*);?url=(.*)~', (string) $response->getHeader('Refresh'), $matchesHeader);
$matches = $isMetaMatch ? $matchesMeta : $matchesHeader;
if (!empty($matches) && (empty($matches[1]) || $matches[1] < $this->refreshMaxInterval)) {
$uri = $this->getAbsoluteUri($matches[2]);
$partsUri = parse_url($uri);
$partsCur = parse_url($this->getHistory()->current()->getUri());
foreach ($partsCur as $key => $part) {
if ($key === 'fragment') {
continue;
}
if (!isset($partsUri[$key]) || $partsUri[$key] !== $part) {
$status = 302;
$headers['Location'] = $uri;
break;
}
}
}
return new BrowserKitResponse($response->getBody(), $status, $headers);
}
示例2: detectEncoding
/**
* @return string
*/
protected function detectEncoding()
{
$encoding = '';
$contentType = $this->response->getHeader('Content-Type');
if ($contentType != '' && strpos($contentType, 'charset') !== FALSE) {
$encoding = $this->detectEncodingOutOfContentTypeString($contentType);
}
if ($encoding === '' && $this->detectMimeType() === 'text/html') {
$encoding = $this->extractContentTypeStringOutOfMetaTag();
}
return $encoding != '' ? strtoupper(trim($encoding)) : $this->defaultEncoding;
}
示例3: verifyHttpResponseHash
protected function verifyHttpResponseHash(Response $httpResponse)
{
$hash = $httpResponse->getHeader('hash');
$responseData = $httpResponse->getBody(true);
$validHash = $this->getHMACMD5Hash($this->getSecret(), $responseData);
$isValid = $validHash == $hash;
return $isValid;
}
示例4: __construct
/**
* Response constructor
*
* @param HttpResponse $httpResponse
* @return self
*/
public function __construct(HttpResponse $httpResponse)
{
if (stripos($httpResponse->getHeader('Content-Type'), 'text/javascript') !== false) {
$this->_response = $this->processJson($httpResponse->getBody());
} else {
$this->_response = $this->processXml($httpResponse->getBody());
}
}
示例5: 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;
}
示例6: parseResponse
/**
* Parse the returned response.
*
* @param \GuzzleHttp\Message\Response $response
* @return array
*
* @throws \RuntimeException
*/
protected function parseResponse(Response $response)
{
$contentType = explode(';', $response->getHeader('content-type'))[0];
switch ($contentType) {
case 'text/javascript':
case 'application/json':
return $response->json();
case 'application/xml':
return $response->xml();
}
throw new \RuntimeException("Unsupported returned content-type [{$contentType}]");
}
示例7: handleResponse
/**
* @param \GuzzleHttp\Message\Response $response
* @return Error
*/
protected function handleResponse($response)
{
$contentType = $response->getHeader('content-type');
if (stripos($contentType, 'application/json') === false) {
return $response;
}
$json = $response->json();
if ($response->getStatusCode() >= 400) {
// oops an error with the response
return new Error($response->getStatusCode(), $json['code'], $json['message']);
}
return $json;
}
示例8: createResponse
/**
* Taken from Mink\BrowserKitDriver
*
* @param Response $response
*
* @return \Symfony\Component\BrowserKit\Response
*/
protected function createResponse(Response $response)
{
$contentType = $response->getHeader('Content-Type');
if (!$contentType or strpos($contentType, 'charset=') === false) {
$body = $response->getBody(true);
if (preg_match('/\\<meta[^\\>]+charset *= *["\']?([a-zA-Z\\-0-9]+)/i', $body, $matches)) {
$contentType .= ';charset=' . $matches[1];
}
$response->setHeader('Content-Type', $contentType);
}
$headers = $response->getHeaders();
$status = $response->getStatusCode();
if (preg_match('/\\<meta[^\\>]+http-equiv="refresh" content=".*?url=(.*?)"/i', $response->getBody(true), $matches)) {
$status = 302;
$headers['Location'] = $matches[1];
}
if (preg_match('~url=(.*)~', (string) $response->getHeader('Refresh'), $matches)) {
$status = 302;
$headers['Location'] = $matches[1];
}
return new BrowserKitResponse($response->getBody(), $status, $headers);
}
示例9: handleResponse
/**
* @param Response $response
* @return array|mixed
* @throws JsonApiResponseException
*/
public function handleResponse($response)
{
$contentType = $response->getHeader('content-type');
// if its not json, then just return the response and handle it in your own object.
if (stripos($contentType, 'application/json') === false) {
return $response;
}
$json = $response->json();
// adobe says hey this didn't work!
if ($response->getStatusCode() >= 400) {
// oops an error with the response, from Adobe complaining about something in your code.
throw new JsonApiResponseException($response->getStatusCode(), $json['message'], $json['code']);
}
return $json;
}
示例10: evaluateResponse
public function evaluateResponse(Response $response)
{
$errors = [];
try {
// Test response code
if (array_key_exists('response.statusCode', $this->expectations)) {
$this->expectations['response.statusCode']->evaluate((int) $response->getStatusCode(), 'Status code was expected to be `%s` but was actually `' . $response->getStatusCode() . '`');
}
} catch (AssertionFailedException $e) {
$errors[] = $e->getMessage();
}
// Test headers
foreach ($this->expectations as $name => $expectation) {
try {
if (substr($name, 0, 8) === 'headers.') {
$headerName = substr($name, 8);
$headerValue = $response->getHeader($headerName);
$this->expectations['headers.' . strtolower($headerName)]->evaluate($headerValue, 'Header `' . $headerName . '` expected to be `%s` but was `%s`');
}
} catch (AssertionFailedException $e) {
$errors[] = $e->getMessage();
}
}
// Test body
$body = json_decode($response->getBody(), true);
foreach ($this->expectations as $name => $expectation) {
try {
if (substr($name, 0, 5) === 'body.') {
$keyName = substr($name, 5);
$keyValue = array_get($body, $keyName);
$this->expectations['body.' . strtolower($keyName)]->evaluate($keyValue, 'Body key `' . $keyName . '` expected to be `%s` but was `%s`');
}
} catch (AssertionFailedException $e) {
$errors[] = $e->getMessage();
}
}
return $errors;
}
示例11: setResponseStream
private function setResponseStream(Response $response, $socket, EmitterInterface $emitter, $useFilter = false)
{
if ($response->getHeader('Transfer-Encoding') == "chunked") {
stream_filter_append($socket, 'dechunk');
}
// Attach filter
if ($useFilter) {
stream_filter_append($socket, 'event', STREAM_FILTER_READ, ['emitter' => $emitter, 'content_type' => $response->getHeader('Content-Type')]);
}
$stream = new Stream($socket);
$response->setBody($stream);
}
示例12: getHeader
/**
* @param $key
* @return string
*/
public function getHeader($key)
{
return $this->response->getHeader($key);
}
示例13: getHeader
/**
* Fetch a named header.
*
* @param string $name
*
* @return string|null
*/
public function getHeader($name)
{
return $this->response->getHeader($name);
}
示例14: setResponseStream
private function setResponseStream(Response $response, $socket)
{
if ($response->getHeader('Transfer-Encoding') == "chunked") {
$stream = new ChunkedStream($socket);
} elseif ($response->getHeader('Content-Type') == "application/vnd.docker.raw-stream") {
$stream = new AttachStream($socket);
} else {
$stream = new Stream($socket);
}
$response->setBody($stream);
}
示例15: detectType
private function detectType(\GuzzleHttp\Message\Response $response)
{
switch ($response->getHeader('content-type')) {
case 'application/json':
return $response->json();
break;
case 'application/xml':
case 'text/xml':
return json_decode(json_encode($response->xml()), true);
break;
default:
return $response->getBody();
}
}