|
1 | | -# HTTP Request |
| 1 | +# HTTP Server Request |
2 | 2 |
|
3 | 3 | [](https://packagist.org/packages/httpsoft/http-request) |
4 | 4 | [](https://packagist.org/packages/httpsoft/http-request) |
|
7 | 7 | [](https://scrutinizer-ci.com/g/httpsoft/http-request/?branch=master) |
8 | 8 | [](https://scrutinizer-ci.com/g/httpsoft/http-request/?branch=master) |
9 | 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. |
| 10 | +This package makes it easy and flexible to create PSR-7 components [ServerRequest](https://github.com/php-fig/http-message/blob/master/src/ServerRequestInterface.php) and [UploadedFile](https://github.com/php-fig/http-message/blob/master/src/UploadedFileInterface.php). |
| 11 | + |
| 12 | +Depends on the [httpsoft/http-message](https://github.com/httpsoft/http-message) package. |
11 | 13 |
|
12 | 14 | ## Documentation |
13 | 15 |
|
14 | | -* [In English language](https://httpsoft.org/docs/request). |
15 | | -* [In Russian language](https://httpsoft.org/ru/docs/request). |
| 16 | +* [In English language](https://httpsoft.org/docs/server-request). |
| 17 | +* [In Russian language](https://httpsoft.org/ru/docs/server-request). |
16 | 18 |
|
17 | 19 | ## Installation |
18 | 20 |
|
19 | 21 | This package requires PHP version 7.4 or later. |
20 | 22 |
|
21 | 23 | ``` |
22 | | -composer require httpsoft/http-request |
| 24 | +composer require httpsoft/http-server-request |
23 | 25 | ``` |
24 | 26 |
|
25 | | -## Usage Request |
| 27 | +## Usage ServerRequestCreator |
26 | 28 |
|
27 | 29 | ```php |
28 | | -use HttpSoft\Request\Request; |
29 | | -use HttpSoft\Request\RequestFactory; |
30 | | - |
31 | | -$request = new Request('POST', 'http://example.com', ['Content-Type' => 'text/html'], 'data://,Content', '2'); |
32 | | -// Or using the factory: |
33 | | -$request = RequestFactory::create( |
34 | | - 'POST', // Request method |
35 | | - 'https://example.com', // Request URI |
36 | | - ['Content-Type' => 'text/html' /* Other headers */], |
37 | | - 'data://,Content', // HTTP message body |
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 | | -``` |
| 30 | +use HttpSoft\ServerRequest\ServerRequestCreator; |
47 | 31 |
|
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 | | - [/* Headers */], |
63 | | - 'php://input', // HTTP message body |
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); |
| 32 | +// All necessary data will be received automatically: |
| 33 | +$request = ServerRequestCreator::createFromGlobals($_SERVER, $_FILES, $_COOKIE, $_GET, $_POST); |
68 | 34 | // equivalently to: |
69 | | -$request = ServerRequestFactory::createFromGlobals(); |
| 35 | +$request = ServerRequestCreator::createFromGlobals(); |
70 | 36 | // 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(); // [] |
| 37 | +$request = ServerRequestCreator::create(); |
84 | 38 | ``` |
85 | 39 |
|
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. |
| 40 | +By default [HttpSoft\ServerRequest\SapiNormalizer](https://github.com/httpsoft/http-server-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\ServerRequest\ServerNormalizerInterface](https://github.com/httpsoft/http-server-request/blob/master/src/ServerNormalizerInterface.php) interface. |
87 | 41 |
|
88 | 42 | ```php |
89 | 43 | $normalizer = new YouCustomServerNormalizer(); |
90 | 44 |
|
91 | | -$request = ServerRequestFactory::create($normalizer); |
| 45 | +$request = ServerRequestCreator::create($normalizer); |
92 | 46 | // 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); |
| 47 | +$request = ServerRequestCreator::createFromGlobals($_SERVER, $_FILES, $_COOKIE, $_GET, $_POST, $normalizer); |
| 48 | +// or with custom superglobals: |
| 49 | +$request = ServerRequestCreator::createFromGlobals($server, $files, $cookie, $get, $post, $normalizer); |
| 50 | +``` |
| 51 | + |
| 52 | +## Usage UploadedFileCreator |
| 53 | + |
| 54 | +```php |
| 55 | +use HttpSoft\ServerRequest\UploadedFileCreator; |
| 56 | + |
| 57 | +/** @var StreamInterface|string|resource $streamOrFile */ |
| 58 | +$uploadedFile = UploadedFileCreator::create($streamOrFile, 1024, UPLOAD_ERR_OK, 'file.txt', 'text/plain'); |
| 59 | + |
| 60 | +// Create a new `HttpSoft\UploadedFile\UploadedFile` instance from array (the item `$_FILES`) |
| 61 | +$uploadedFile = UploadedFileCreator::createFromArray([ |
| 62 | + 'name' => 'filename.jpg', // optional |
| 63 | + 'type' => 'image/jpeg', // optional |
| 64 | + 'tmp_name' => '/tmp/php/php6hst32', |
| 65 | + 'error' => 0, // UPLOAD_ERR_OK |
| 66 | + 'size' => 98174, |
| 67 | +]); |
| 68 | + |
| 69 | +// Normalizes the superglobal structure and converts each array |
| 70 | +// value to an instance of `Psr\Http\Message\UploadedFileInterface`. |
| 71 | +$uploadedFiles = UploadedFileCreator::createFromGlobals($_FILES); |
96 | 72 | ``` |
0 commit comments