本文整理汇总了PHP中Psr\Http\Message\RequestInterface::getHeaders方法的典型用法代码示例。如果您正苦于以下问题:PHP RequestInterface::getHeaders方法的具体用法?PHP RequestInterface::getHeaders怎么用?PHP RequestInterface::getHeaders使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Psr\Http\Message\RequestInterface
的用法示例。
在下文中一共展示了RequestInterface::getHeaders方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: renderRequest
/**
* Render a PSR-7 request.
*
* @param RequestInterface $request
* @return string
*/
public function renderRequest(RequestInterface $request)
{
$return = '';
$return .= sprintf("URL: %s\n", $request->getUri());
$return .= sprintf("METHOD: %s\n", $request->getMethod());
if ($request->getHeaders()) {
$return .= 'HEADERS:';
}
$indent = false;
foreach ($request->getHeaders() as $name => $values) {
if ($indent) {
$return .= str_repeat(' ', 8);
}
$return .= sprintf(" %s: %s\n", $name, implode(', ', $values));
$indent = true;
}
if ($body = (string) $request->getBody()) {
$return .= 'BODY: ';
switch ($request->getHeaderLine('Content-Type')) {
case 'application/json':
$return .= json_encode(json_decode($body, true), JSON_PRETTY_PRINT);
break;
default:
$return .= $body;
break;
}
$return .= "\n";
}
return $return;
}
示例2: __invoke
/**
* @param Request $request
*
* @return Response
*/
public function __invoke(Request $request)
{
$options = [];
// Headers
$headerLines = [];
foreach ($request->getHeaders() as $name => $values) {
$headerLines[] = sprintf('%s: %s', $name, implode(', ', $values));
}
$options[CURLOPT_HTTPHEADER] = $headerLines;
// Url
$options[CURLOPT_URL] = (string) $request->getUri();
switch ($request->getMethod()) {
case 'HEAD':
$options[CURLOPT_NOBODY] = true;
break;
case 'GET':
$options[CURLOPT_HTTPGET] = true;
break;
case 'POST':
$options[CURLOPT_POST] = true;
$options[CURLOPT_POSTFIELDS] = (string) $request->getBody();
// Don't duplicate the Content-Length header
$request = $request->withoutHeader('Content-Length');
$request = $request->withoutHeader('Transfer-Encoding');
break;
case 'PUT':
// Write to memory/temp
$file = fopen('php://temp/' . spl_object_hash($request), 'w+');
$bytes = fwrite($file, (string) $request->getBody());
rewind($file);
$options[CURLOPT_PUT] = true;
$options[CURLOPT_INFILE] = $file;
$options[CURLOPT_INFILESIZE] = $bytes;
$request = $request->withoutHeader('Content-Length');
break;
default:
$options[CURLOPT_CUSTOMREQUEST] = $request->getMethod();
}
// If the Expect header is not present, prevent curl from adding it
if (!$request->hasHeader('Expect')) {
$options[CURLOPT_HTTPHEADER][] = 'Expect:';
}
// cURL sometimes adds a content-type by default. Prevent this.
if (!$request->hasHeader('Content-Type')) {
$options[CURLOPT_HTTPHEADER][] = 'Content-Type:';
}
list($body, $headerLines) = $this->execute($options);
$headerLines = preg_split("#\r\n#", $headerLines, -1, PREG_SPLIT_NO_EMPTY);
$headers = [];
// Extract the version and status from the first header
preg_match('#HTTP/(\\d\\.\\d)\\s(\\d\\d\\d)\\s(.*)#', array_shift($headerLines), $matches);
array_shift($matches);
list($protocolVersion, $statusCode, $reasonPhrase) = $matches;
foreach ($headerLines as $line) {
list($name, $values) = preg_split('#\\s*:\\s*#', $line, 2, PREG_SPLIT_NO_EMPTY);
$headers[$name] = preg_split('#\\s*,\\s*#', $values, -1, PREG_SPLIT_NO_EMPTY);
}
$response = new \GuzzleHttp\Psr7\Response($statusCode, $headers, $body, $protocolVersion, $reasonPhrase);
return $response;
}
示例3:
function it_returns_body_with_headers(RequestInterface $request, Stream $stream)
{
$request->getHeaders()->shouldBeCalled()->willReturn(['content-type' => 'application/json']);
$request->getBody()->shouldBeCalled()->willReturn($stream);
$stream->getContents()->shouldBeCalled()->willReturn('{"data": {}}');
$this->getBodyWithHeaders($request)->shouldReturn('content-type: application/json' . PHP_EOL . PHP_EOL . '{"data": {}}');
}
示例4: request
/**
* {@inheritdoc}
*/
public function request(RequestInterface $request)
{
$url = (string) $request->getUri();
$body = $request->getBody();
$body->seek(0);
$headers = $request->getHeaders();
$headers['Accept'] = 'application/json';
$headers['Content-Type'] = 'application/json';
$req = $this->guzzle->createRequest($request->getMethod(), $url);
$req->setHeaders($headers);
$req->setBody(GStream::factory($body->getContents()));
try {
$res = $this->guzzle->send($req);
} catch (RequestException $e) {
// Guzzle will throw exceptions for 4xx and 5xx responses, so we catch
// them here and quietly get the response object.
$res = $e->getResponse();
if (!$res) {
throw $e;
}
}
$response = (new Response(new Stream('php://memory', 'w')))->withStatus($res->getStatusCode(), $res->getReasonPhrase());
$response->getBody()->write((string) $res->getBody());
return $response;
}
示例5: assertRequest
/**
* Assert request matches against declared specification.
*
* The list of constraints:
*
* - Assert request method defined
* - Assert request URI declared by host, basePath, schemes and parameters (path, query)
* - Assert content-type declared by consumes
* - Assert headers declared by parameters (header)
* - Assert body declared by parameters (body)
*
* @param Spec $spec
* @param string $template
* @param Request $request
* @param string $msg
*/
protected static final function assertRequest(Spec $spec, $template, Request $request, $msg = '')
{
self::assertMethodAllowed($spec, $template, $request->getMethod(), $msg);
self::assertUri($spec, $template, $request->getMethod(), $request->getUri(), $msg);
self::assertRequestHeaders($spec, $template, $request->getMethod(), $request->getHeaders(), $msg);
self::assertRequestBody($spec, $template, $request->getMethod(), $request->getBody(), $msg);
}
示例6: send
/**
* @param RequestInterface $request
* @return ResponseInterface
*/
public function send($request)
{
/**
* var \GuzzleHttp\Message\Response $response
*/
$headers = $request->getHeaders();
if (!empty($this->append_headers)) {
$headers = array_merge($headers, $this->append_headers);
}
$opt = [];
if (!empty($this->basicAuth)) {
$opt['auth'] = $this->basicAuth;
}
if (!empty($headers)) {
$opt['headers'] = $headers;
}
$body = $request->getBody();
if ($body !== null) {
$opt['body'] = $body;
}
$g4request = $this->getClient()->createRequest($request->getMethod(), $request->getUri(), $opt);
try {
$response = $this->getClient()->send($g4request);
return new Response($response->getStatusCode(), $response->getHeaders(), $response->getBody());
} catch (\GuzzleHttp\Exception\RequestException $ex) {
$ex_request = $ex->getRequest();
$ex_response = $ex->getResponse();
throw new RequestException($ex->getMessage(), $ex_request ? new Request($ex_request->getMethod(), $ex_request->getUrl(), $ex_request->getHeaders(), $ex_request->getBody()) : null, $ex_response ? new Response($ex_response->getStatusCode(), $ex_response->getHeaders(), $ex_response->getBody()) : null, $ex);
}
}
示例7: detect
public function detect()
{
if ($this->detected === null) {
$headers = $this->request->getHeaders();
$userAgent = $this->request->getHeaderLine('user-agent');
if ($this->detector->isMobile($headers, $userAgent)) {
$this->detected = 'mobile';
} else {
if ($this->detector->isTablet($headers, $userAgent)) {
$this->detected = 'tablet';
} else {
$this->detected = 'desktop';
}
}
}
return $this->detected;
}
示例8: getRequestHeaders
/**
* @param RequestInterface $request
* @return string[]
*/
protected function getRequestHeaders(RequestInterface $request)
{
$headers = array();
foreach (array_keys($request->getHeaders()) as $name) {
$headers[] = $name . ': ' . $request->getHeaderLine($name);
}
return $headers;
}
示例9: setupRequest
/**
* @return HttpRequest mixed
*/
protected function setupRequest()
{
$headers = [];
foreach ($this->request->getHeaders() as $key => $values) {
$headers[$key] = implode(';', $values);
}
return $this->httpClient->request($this->request->getMethod(), (string) $this->request->getUri(), $headers, $this->request->getProtocolVersion());
}
示例10: formatRequest
/**
* {@inheritdoc}
*/
public function formatRequest(RequestInterface $request)
{
$message = sprintf("%s %s HTTP/%s\n", $request->getMethod(), $request->getRequestTarget(), $request->getProtocolVersion());
foreach ($request->getHeaders() as $name => $values) {
$message .= $name . ': ' . implode(', ', $values) . "\n";
}
return $this->addBody($request, $message);
}
示例11: createRequest
/**
* Converts a PSR request into a Guzzle request.
*
* @param RequestInterface $request
*
* @return GuzzleRequest
*/
private function createRequest(RequestInterface $request)
{
$options = ['exceptions' => false, 'allow_redirects' => false];
$options['version'] = $request->getProtocolVersion();
$options['headers'] = $request->getHeaders();
$options['body'] = (string) $request->getBody();
return $this->client->createRequest($request->getMethod(), (string) $request->getUri(), $options);
}
示例12: generate
/**
* Generates headers for given request
* @param RequestInterface $request
* @return array
*/
public function generate(RequestInterface $request)
{
$headers = [];
foreach ($request->getHeaders() as $name => $values) {
$headers[] = sprintf('%s: %s', $name, implode(', ', $values));
}
$headers[] = self::CHUNKED_TRANSFER_HEADER;
return $headers;
}
示例13: sign
public function sign(RequestInterface $request, $key, $secret, $customerId = null)
{
$nonce = $this->nonceGenerator->generateNonce();
$signature = hash_hmac('sha256', $nonce . $customerId . $key, $secret);
parse_str($request->getBody()->getContents(), $params);
$params = array_merge(['key' => $key, 'nonce' => $nonce, 'signature' => $signature], $params);
$body = http_build_query($params);
return new Request($request->getMethod(), $request->getUri(), $request->getHeaders(), $body);
}
示例14: request
public static function request(RequestInterface $request)
{
$method = $request->getMethod();
$url = $request->getUri();
$body = $request->getBody()->getContents();
$headers = $request->getHeaders();
$protocolVersion = $request->getProtocolVersion();
return new HttpObservable($method, $url, $body, $headers, $protocolVersion);
}
示例15: transformRequestHeadersToString
/**
* Produce the header of request as a string based on a PSR Request
*
* @param RequestInterface $request
*
* @return string
*/
protected function transformRequestHeadersToString(RequestInterface $request)
{
$message = vsprintf('%s %s HTTP/%s', [strtoupper($request->getMethod()), $request->getRequestTarget(), $request->getProtocolVersion()]) . "\r\n";
foreach ($request->getHeaders() as $name => $values) {
$message .= $name . ': ' . implode(', ', $values) . "\r\n";
}
$message .= "\r\n";
return $message;
}