sunrise / http-message
HTTP message wrapper for PHP 7.4+ based on RFC-7230, PSR-7 and PSR-17
Installs: 99 470
Dependents: 16
Suggesters: 0
Security: 0
Stars: 12
Watchers: 3
Forks: 2
Open Issues: 3
Requires
- php: >=7.4
- fig/http-message-util: ^1.1
- psr/http-factory: ^1.0
- psr/http-message: ^1.0
Requires (Dev)
- php-http/psr7-integration-tests: ^1.4
- phpstan/phpstan: ^1.12
- phpunit/phpunit: ^9.6
- sunrise/coding-standard: ^1.0
- vimeo/psalm: ^5.26
Provides
- dev-master
- v3.2.0
- v3.1.0
- v3.1.0-rc.2
- v3.1.0-rc.1
- v3.0.0
- v2.1.3
- v2.1.2
- v2.1.1
- v2.1.0
- v2.0.0
- v1.4.0
- v1.3.0
- v1.2.0
- v1.1.0
- v1.0.16
- v1.0.15
- v1.0.14
- v1.0.13
- v1.0.12
- v1.0.11
- v1.0.10
- v1.0.9
- v1.0.8
- v1.0.7
- v1.0.6
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- dev-renovate/major-phpstan-packages
- dev-release/v3.2
- dev-release/v3.1.0
This package is auto-updated.
Last update: 2024-11-11 07:26:12 UTC
README
Installation
composer require sunrise/http-message
How to Use
We highly recommend studying PSR-7 and PSR-17, as only basic examples are provided below.
Server Request from Global Environment
$request = \Sunrise\Http\Message\ServerRequestFactory::fromGlobals();
Typed Messages
JSON Request
$request = new \Sunrise\Http\Message\Request\JsonRequest('POST', '/', ['foo' => 'bar']);
You can also specify encoding flags and the maximum nesting depth as shown below:
$request = new \Sunrise\Http\Message\Request\JsonRequest('POST', '/', [], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE, 512);
URL Encoded Request
$request = new \Sunrise\Http\Message\Request\UrlEncodedRequest('POST', '/', ['foo' => 'bar']);
You can also specify the encoding type as shown below:
$rfc1738 = \Sunrise\Http\Message\Request\UrlEncodedRequest::ENCODING_TYPE_RFC1738; $request = new \Sunrise\Http\Message\Request\UrlEncodedRequest('POST', '/', [], $rfc1738);
$rfc3986 = \Sunrise\Http\Message\Request\UrlEncodedRequest::ENCODING_TYPE_RFC3986; $request = new \Sunrise\Http\Message\Request\UrlEncodedRequest('POST', '/', [], $rfc3986);
JSON Response
$response = new \Sunrise\Http\Message\Response\JsonResponse(200, ['foo' => 'bar']);
You can also specify encoding flags and the maximum nesting depth as shown below:
$response = new \Sunrise\Http\Message\Response\JsonResponse(200, [], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE, 512);
HTML Response
$response = new \Sunrise\Http\Message\Response\HtmlResponse(200, '<h1>Welcome!</h1>');
Streams
File Stream
$stream = new \Sunrise\Http\Message\Stream\FileStream('/folder/file', 'r+b');
Input Stream
More details about this stream can be found on the official page.
$stream = new \Sunrise\Http\Message\Stream\PhpInputStream();
Memory Stream
More details about this stream can be found on the official page.
$stream = new \Sunrise\Http\Message\Stream\PhpMemoryStream('r+b');
Temporary Stream
More details about this stream can be found on the official page.
$stream = new \Sunrise\Http\Message\Stream\PhpTempStream('r+b');
You can also specify a memory limit. When this limit is reached, PHP will switch to using a temporary file instead of memory.
Please note that the default memory limit is 2MB.
$stream = new \Sunrise\Http\Message\Stream\PhpTempStream('r+b', 1e+6);
Temporary File Stream
For more details about the behavior of temporary files, visit the official page.
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.
$stream = new \Sunrise\Http\Message\Stream\TmpfileStream(); $stream->getMetadata('uri'); // the file path
If you don't require the behavior described above, you can use an alternative temporary file stream:
$stream = new \Sunrise\Http\Message\Stream\TempFileStream(); $stream->getMetadata('uri'); // the file path
PSR-7 and PSR-17
The following classes are implementations PSR-7:
Sunrise\Http\Message\Request
Sunrise\Http\Message\Response
Sunrise\Http\Message\ServerRequest
Sunrise\Http\Message\Stream
Sunrise\Http\Message\UploadedFile
Sunrise\Http\Message\Uri
The following classes are implementations PSR-17:
Sunrise\Http\Message\RequestFactory
Sunrise\Http\Message\ResponseFactory
Sunrise\Http\Message\ServerRequestFactory
Sunrise\Http\Message\StreamFactory
Sunrise\Http\Message\UploadedFileFactory
Sunrise\Http\Message\UriFactory
Error Handling
Any exceptions thrown by this package can be caught through the following interface:
try { } catch (\Sunrise\Http\Message\Exception\ExceptionInterface $e) { }
Test Run
composer test