|
1 | 1 | # HTTP Request |
2 | 2 |
|
3 | | -The repository is under development, and the description will appear later. |
| 3 | +[](https://packagist.org/packages/httpsoft/http-request) |
| 4 | +[](https://packagist.org/packages/httpsoft/http-request) |
| 5 | +[](https://packagist.org/packages/httpsoft/http-request) |
| 6 | +[](https://github.com/httpsoft/http-request/actions) |
| 7 | +[](https://scrutinizer-ci.com/g/httpsoft/http-request/?branch=master) |
| 8 | +[](https://scrutinizer-ci.com/g/httpsoft/http-request/?branch=master) |
| 9 | + |
| 10 | +This package implements [Psr\Http\Message\RequestInterface](https://github.com/php-fig/http-message/blob/master/src/RequestInterface.php) and [Psr\Http\Message\ServerRequestInterface](https://github.com/php-fig/http-message/blob/master/src/ServerRequestInterface.php) from [PSR-7 HTTP Message](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md) in accordance with the [RFC 7230](https://tools.ietf.org/html/rfc7230) and [RFC 7231](https://tools.ietf.org/html/rfc7231) specifications. |
| 11 | + |
| 12 | +## Documentation |
| 13 | + |
| 14 | +* [In English language](https://httpsoft.org/docs/request). |
| 15 | +* [In Russian language](https://httpsoft.org/ru/docs/request). |
| 16 | + |
| 17 | +## Installation |
| 18 | + |
| 19 | +This package requires PHP version 7.4 or later. |
| 20 | + |
| 21 | +``` |
| 22 | +composer require httpsoft/http-request |
| 23 | +``` |
| 24 | + |
| 25 | +## Usage Request |
| 26 | + |
| 27 | +```php |
| 28 | +use HttpSoft\Request\Request; |
| 29 | +use HttpSoft\Request\RequestFactory; |
| 30 | + |
| 31 | +$request = new Request('POST', 'http://example.com', 'data://,Content', ['Content-Type' => 'text/html'], '2'); |
| 32 | +// Or using the factory: |
| 33 | +$request = RequestFactory::create( |
| 34 | + 'POST', // Request method |
| 35 | + 'https://example.com', // Request URI |
| 36 | + 'data://,Content', // HTTP message body |
| 37 | + ['Content-Type' => 'text/html' /* Other headers */], |
| 38 | + '2' // HTTP protocol version |
| 39 | +); |
| 40 | + |
| 41 | +$request->getMethod(); // 'POST' |
| 42 | +$request->getProtocolVersion(); // '2' |
| 43 | +$request->getBody()->getContents(); // 'Content' |
| 44 | +(string) $request->getUri(); // 'https://example.com/path' |
| 45 | +$request->getHeaders(); // ['Host' => ['example.com'], 'Content-Type' => ['text/html']] |
| 46 | +``` |
| 47 | + |
| 48 | +## Usage ServerRequest |
| 49 | + |
| 50 | +```php |
| 51 | +use HttpSoft\Request\ServerRequest; |
| 52 | +use HttpSoft\Request\ServerRequestFactory; |
| 53 | + |
| 54 | +$request = new ServerRequest( |
| 55 | + $_SERVER, |
| 56 | + $_FILES, |
| 57 | + $_COOKIE, |
| 58 | + $_GET, |
| 59 | + $_POST, |
| 60 | + 'GET', // Request method |
| 61 | + 'https://example.com', // Request URI |
| 62 | + 'php://input', // HTTP message body |
| 63 | + [/* Headers */], |
| 64 | + '2' // HTTP protocol version |
| 65 | +); |
| 66 | +// Or using the factory (all necessary data will be received automatically): |
| 67 | +$request = ServerRequestFactory::createFromGlobals($_SERVER, $_FILES, $_COOKIE, $_GET, $_POST); |
| 68 | +// equivalently to: |
| 69 | +$request = ServerRequestFactory::createFromGlobals(); |
| 70 | +// equivalently to: |
| 71 | +$request = ServerRequestFactory::create(); |
| 72 | + |
| 73 | +$request->getMethod(); // 'GET' |
| 74 | +$request->getProtocolVersion(); // '2' |
| 75 | +$request->getBody()->getContents(); // '' |
| 76 | +(string) $request->getUri(); // 'https://example.com' |
| 77 | +$request->getHeaders(); // ['Host' => ['example.com']] |
| 78 | +$request->getServerParams(); // $_SERVER |
| 79 | +$request->getUploadedFiles(); // $_FILES |
| 80 | +$request->getCookieParams(); // $_COOKIE |
| 81 | +$request->getQueryParams(); // $_GET |
| 82 | +$request->getParsedBody(); // $_POST |
| 83 | +$request->getAttributes(); // [] |
| 84 | +``` |
| 85 | + |
| 86 | +By default [HttpSoft\Request\SapiNormalizer](https://github.com/httpsoft/http-request/blob/master/src/SapiNormalizer.php) is used for normalization of server parameters. You can use your own server parameters normalizer, for this you need to implement an [HttpSoft\Request\ServerNormalizerInterface](https://github.com/httpsoft/http-request/blob/master/src/ServerNormalizerInterface.php) interface. |
| 87 | + |
| 88 | +```php |
| 89 | +$normalizer = new YouCustomServerNormalizer(); |
| 90 | + |
| 91 | +$request = ServerRequestFactory::create($normalizer); |
| 92 | +// equivalently to: |
| 93 | +$request = ServerRequestFactory::createFromGlobals($_SERVER, $_FILES, $_COOKIE, $_GET, $_POST, $normalizer); |
| 94 | +// or with custom superglobals |
| 95 | +$request = ServerRequestFactory::createFromGlobals($server, $files, $cookie, $get, $post, $normalizer); |
| 96 | +``` |
0 commit comments