sunrise / http-message
HTTP message wrapper for PHP 7.4+ based on RFC-7230, PSR-7 and PSR-17
Installs: 98 232
Dependents: 16
Suggesters: 0
Security: 0
Stars: 12
Watchers: 3
Forks: 2
Open Issues: 5
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.1
- phpunit/phpunit: ~9.5.0
- sunrise/coding-standard: ~1.0.0
Provides
- dev-master
- 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/phpunit-phpunit-11.x
- dev-renovate/psr-http-message-2.x
- dev-renovate/phpunit-phpunit-9.x
This package is auto-updated.
Last update: 2024-09-02 04:11:19 UTC
README
Installation
composer require sunrise/http-message
Documentation navigation
How to use
We highly recommend that you study PSR-7 and PSR-17 because only superficial examples will be presented below.
Server request from global environment
use Sunrise\Http\Message\ServerRequestFactory; $request = ServerRequestFactory::fromGlobals();
HTML and JSON responses
HTML response
use Sunrise\Http\Message\Response\HtmlResponse; /** @var $html string|Stringable */ $response = new HtmlResponse(200, $html);
JSON response
use Sunrise\Http\Message\Response\JsonResponse; /** @var $data mixed */ $response = new JsonResponse(200, $data);
You can also specify encoding flags and maximum nesting depth like below:
$response = new JsonResponse(200, $data, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE, 512);
Streams
File stream
use Sunrise\Http\Message\Stream\FileStream; $fileStream = new FileStream('/folder/file', 'r+b');
PHP input stream
More details about the stream at the official page.
use Sunrise\Http\Message\Stream\PhpInputStream; $inputStream = new PhpInputStream();
PHP memory stream
More details about the stream at the official page.
use Sunrise\Http\Message\Stream\PhpMemoryStream; $memoryStream = new PhpMemoryStream('r+b');
PHP temporary stream
More details about the stream at the official page.
use Sunrise\Http\Message\Stream\PhpTempStream; $tempStream = new PhpTempStream('r+b');
You can also specify the memory limit, when the limit is reached, PHP will start using the temporary file instead of memory.
Please note that the default memory limit is 2MB.
$maxMemory = 1e+6; // 1MB $tempStream = new PhpTempStream('r+b', $maxMemory);
Temporary file stream
More details about the temporary file behaviour at the official page.
The stream opens a unique temporary file in binary read/write (w+b) mode. The file will be automatically deleted when it is closed or the program terminates.
use Sunrise\Http\Message\Stream\TmpfileStream; $tmpfileStream = new TmpfileStream(); // Returns the file path... $tmpfileStream->getMetadata('uri');
If you don't need the above behavior, you can use another temporary file stream:
use Sunrise\Http\Message\Stream\TempFileStream; $tempFileStream = new TempFileStream(); // Returns the file path... $tempFileStream->getMetadata('uri');
PSR-7 and PSR-17
The following classes implement 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 implement 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
Exceptions
Any exceptions of this package can be caught through the interface:
use Sunrise\Http\Message\Exception\ExceptionInterface; try { // some code... } catch (ExceptionInterface $e) { // some logic... }
Test run
composer test