當前位置: 首頁>>代碼示例>>PHP>>正文


PHP RequestInterface::getHeaders方法代碼示例

本文整理匯總了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;
 }
開發者ID:php-school,項目名稱:php-workshop,代碼行數:36,代碼來源:RequestRenderer.php

示例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;
 }
開發者ID:Teknologica,項目名稱:rebilly-php,代碼行數:65,代碼來源:CurlHandler.php

示例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": {}}');
 }
開發者ID:rawphp,項目名稱:communication-logger,代碼行數:7,代碼來源:HttpParserSpec.php

示例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;
 }
開發者ID:AnnaCaraman,項目名稱:ding2,代碼行數:28,代碼來源:FBSGuzzleHttpClient.php

示例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);
 }
開發者ID:rebilly,項目名稱:openapi-php,代碼行數:23,代碼來源:Asserts.php

示例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);
     }
 }
開發者ID:cdyweb,項目名稱:http-adapter,代碼行數:34,代碼來源:Guzzle4.php

示例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;
 }
開發者ID:maboiteaspam,項目名稱:im-device-detect,代碼行數:17,代碼來源:MobileDetect.php

示例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;
 }
開發者ID:ringcentral,項目名稱:ringcentral-php,代碼行數:12,代碼來源:Client.php

示例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());
 }
開發者ID:wyrihaximus,項目名稱:react-guzzle-http-client,代碼行數:11,代碼來源:Request.php

示例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);
 }
開發者ID:php-http,項目名稱:message,代碼行數:11,代碼來源:FullHttpMessageFormatter.php

示例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);
 }
開發者ID:parsingeye,項目名稱:guzzle5-adapter,代碼行數:15,代碼來源:Guzzle5HttpAdapter.php

示例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;
 }
開發者ID:zloesabo,項目名稱:speechkit-php,代碼行數:14,代碼來源:HeaderGenerator.php

示例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);
 }
開發者ID:softwareontherocks,項目名稱:cryptosdks,代碼行數:9,代碼來源:BitstampRequestSigner.php

示例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);
 }
開發者ID:RxPHP,項目名稱:RxHttp,代碼行數:9,代碼來源:Http.php

示例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;
 }
開發者ID:printu,項目名稱:socket-client,代碼行數:16,代碼來源:RequestWriter.php


注:本文中的Psr\Http\Message\RequestInterface::getHeaders方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。