|
1 | | -# HTTP message wrapper for PHP 7.4+, based on RFC-7230, PSR-7, and PSR-17. |
| 1 | +# An HTTP message implementation based on PSR-7, PSR-17 and RFC-7230 |
2 | 2 |
|
3 | | -[](https://scrutinizer-ci.com/g/sunrise-php/http-message/build-status/master) |
4 | | -[](https://scrutinizer-ci.com/g/sunrise-php/http-message/?branch=master) |
5 | | -[](https://scrutinizer-ci.com/g/sunrise-php/http-message/?branch=master) |
6 | | -[](https://packagist.org/packages/sunrise/http-message) |
7 | | -[](https://packagist.org/packages/sunrise/http-message) |
8 | | -[](https://packagist.org/packages/sunrise/http-message) |
| 3 | +## Resources |
9 | 4 |
|
10 | | ---- |
11 | | - |
12 | | -## Installation |
13 | | - |
14 | | -```bash |
15 | | -composer require sunrise/http-message |
16 | | -``` |
17 | | - |
18 | | -## How to Use |
19 | | - |
20 | | -We highly recommend studying [PSR-7](https://www.php-fig.org/psr/psr-7/) and [PSR-17](https://www.php-fig.org/psr/psr-17/), as only basic examples are provided below. |
21 | | - |
22 | | -### Server Request from Global Environment |
23 | | - |
24 | | -```php |
25 | | -$request = \Sunrise\Http\Message\ServerRequestFactory::fromGlobals(); |
26 | | -``` |
27 | | - |
28 | | -### Typed Messages |
29 | | - |
30 | | -#### JSON Request |
31 | | - |
32 | | -```php |
33 | | -$request = new \Sunrise\Http\Message\Request\JsonRequest('POST', '/', ['foo' => 'bar']); |
34 | | -``` |
35 | | - |
36 | | -You can also specify [encoding flags](https://www.php.net/manual/en/json.constants.php#constant.json-hex-tag) and the maximum nesting depth as shown below: |
37 | | - |
38 | | -```php |
39 | | -$request = new \Sunrise\Http\Message\Request\JsonRequest('POST', '/', [], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE, 512); |
40 | | -``` |
41 | | - |
42 | | -#### URL Encoded Request |
43 | | - |
44 | | -```php |
45 | | -$request = new \Sunrise\Http\Message\Request\UrlEncodedRequest('POST', '/', ['foo' => 'bar']); |
46 | | -``` |
47 | | - |
48 | | -You can also specify the [encoding type](https://www.php.net/manual/ru/url.constants.php#constant.php-query-rfc1738) as shown below: |
49 | | - |
50 | | -```php |
51 | | -$rfc1738 = \Sunrise\Http\Message\Request\UrlEncodedRequest::ENCODING_TYPE_RFC1738; |
52 | | -$request = new \Sunrise\Http\Message\Request\UrlEncodedRequest('POST', '/', [], $rfc1738); |
53 | | -``` |
54 | | - |
55 | | -```php |
56 | | -$rfc3986 = \Sunrise\Http\Message\Request\UrlEncodedRequest::ENCODING_TYPE_RFC3986; |
57 | | -$request = new \Sunrise\Http\Message\Request\UrlEncodedRequest('POST', '/', [], $rfc3986); |
58 | | -``` |
59 | | - |
60 | | -#### JSON Response |
61 | | - |
62 | | -```php |
63 | | -$response = new \Sunrise\Http\Message\Response\JsonResponse(200, ['foo' => 'bar']); |
64 | | -``` |
65 | | - |
66 | | -You can also specify [encoding flags](https://www.php.net/manual/en/json.constants.php#constant.json-hex-tag) and the maximum nesting depth as shown below: |
67 | | - |
68 | | -```php |
69 | | -$response = new \Sunrise\Http\Message\Response\JsonResponse(200, [], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE, 512); |
70 | | -``` |
71 | | - |
72 | | -#### HTML Response |
73 | | - |
74 | | -```php |
75 | | -$response = new \Sunrise\Http\Message\Response\HtmlResponse(200, '<h1>Welcome!</h1>'); |
76 | | -``` |
77 | | - |
78 | | -### Streams |
79 | | - |
80 | | -#### File Stream |
81 | | - |
82 | | -```php |
83 | | -$stream = new \Sunrise\Http\Message\Stream\FileStream('/folder/file', 'r+b'); |
84 | | -``` |
85 | | - |
86 | | -#### Input Stream |
87 | | - |
88 | | -More details about this stream can be found on the [official page](https://www.php.net/manual/en/wrappers.php.php#wrappers.php.input). |
89 | | - |
90 | | -```php |
91 | | -$stream = new \Sunrise\Http\Message\Stream\PhpInputStream(); |
92 | | -``` |
93 | | - |
94 | | -#### Memory Stream |
95 | | - |
96 | | -More details about this stream can be found on the [official page](https://www.php.net/manual/en/wrappers.php.php#wrappers.php.input). |
97 | | - |
98 | | -```php |
99 | | -$stream = new \Sunrise\Http\Message\Stream\PhpMemoryStream('r+b'); |
100 | | -``` |
101 | | - |
102 | | -#### Temporary Stream |
103 | | - |
104 | | -More details about this stream can be found on the [official page](https://www.php.net/manual/en/wrappers.php.php#wrappers.php.input). |
105 | | - |
106 | | -```php |
107 | | -$stream = new \Sunrise\Http\Message\Stream\PhpTempStream('r+b'); |
108 | | -``` |
109 | | - |
110 | | -You can also specify a memory limit. When this limit is reached, PHP will switch to using a temporary file instead of memory. |
111 | | - |
112 | | -> Please note that the default memory limit is 2MB. |
113 | | -
|
114 | | -```php |
115 | | -$stream = new \Sunrise\Http\Message\Stream\PhpTempStream('r+b', 1e+6); |
116 | | -``` |
117 | | - |
118 | | -#### Temporary File Stream |
119 | | - |
120 | | -For more details about the behavior of temporary files, visit [the official page](https://www.php.net/manual/en/function.tmpfile). |
121 | | - |
122 | | -The stream opens a unique temporary file in binary read/write mode (w+b). The file will be automatically deleted when it is closed or when the program terminates. |
123 | | - |
124 | | -```php |
125 | | -$stream = new \Sunrise\Http\Message\Stream\TmpfileStream(); |
126 | | -$stream->getMetadata('uri'); // the file path |
127 | | -``` |
128 | | - |
129 | | -If you don't require the behavior described above, you can use an alternative temporary file stream: |
130 | | - |
131 | | -```php |
132 | | -$stream = new \Sunrise\Http\Message\Stream\TempFileStream(); |
133 | | -$stream->getMetadata('uri'); // the file path |
134 | | -``` |
135 | | - |
136 | | -### PSR-7 and PSR-17 |
137 | | - |
138 | | -The following classes are implementations PSR-7: |
139 | | - |
140 | | -- `Sunrise\Http\Message\Request` |
141 | | -- `Sunrise\Http\Message\Response` |
142 | | -- `Sunrise\Http\Message\ServerRequest` |
143 | | -- `Sunrise\Http\Message\Stream` |
144 | | -- `Sunrise\Http\Message\UploadedFile` |
145 | | -- `Sunrise\Http\Message\Uri` |
146 | | - |
147 | | -The following classes are implementations PSR-17: |
148 | | - |
149 | | -- `Sunrise\Http\Message\RequestFactory` |
150 | | -- `Sunrise\Http\Message\ResponseFactory` |
151 | | -- `Sunrise\Http\Message\ServerRequestFactory` |
152 | | -- `Sunrise\Http\Message\StreamFactory` |
153 | | -- `Sunrise\Http\Message\UploadedFileFactory` |
154 | | -- `Sunrise\Http\Message\UriFactory` |
155 | | - |
156 | | -### Error Handling |
157 | | - |
158 | | -Any exceptions thrown by this package can be caught through the following interface: |
159 | | - |
160 | | -```php |
161 | | -try { |
162 | | -} catch (\Sunrise\Http\Message\Exception\ExceptionInterface $e) { |
163 | | -} |
164 | | -``` |
165 | | - |
166 | | ---- |
167 | | - |
168 | | -## Test Run |
169 | | - |
170 | | -```bash |
171 | | -composer test |
172 | | -``` |
173 | | - |
174 | | -## Useful Links |
175 | | - |
176 | | -- https://tools.ietf.org/html/rfc7230 |
177 | | -- https://www.php-fig.org/psr/psr-7/ |
178 | | -- https://www.php-fig.org/psr/psr-17/ |
| 5 | +- [Documentation](https://dev.sunrise-studio.io/docs/packages/sunrise/http-message/) |
0 commit comments