Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sunrise-php/http-message
HTTP message wrapper for PHP 7.4+ based on RFC-7230, PSR-7 & PSR-17
https://github.com/sunrise-php/http-message
header http http-message message-factory php-library php7 php8 psr-17 psr-7 request response rfc-7230 stream uri
Last synced: 5 days ago
JSON representation
HTTP message wrapper for PHP 7.4+ based on RFC-7230, PSR-7 & PSR-17
- Host: GitHub
- URL: https://github.com/sunrise-php/http-message
- Owner: sunrise-php
- License: mit
- Created: 2018-11-12T13:05:55.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2024-10-30T02:25:59.000Z (15 days ago)
- Last Synced: 2024-10-30T03:53:29.083Z (15 days ago)
- Topics: header, http, http-message, message-factory, php-library, php7, php8, psr-17, psr-7, request, response, rfc-7230, stream, uri
- Language: PHP
- Homepage:
- Size: 284 KB
- Stars: 12
- Watchers: 3
- Forks: 2
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-psr15-middlewares - sunrise/http-message
README
# HTTP message wrapper for PHP 7.4+, based on RFC-7230, PSR-7, and PSR-17.
[![Build Status](https://scrutinizer-ci.com/g/sunrise-php/http-message/badges/build.png?b=master)](https://scrutinizer-ci.com/g/sunrise-php/http-message/build-status/master)
[![Code Coverage](https://scrutinizer-ci.com/g/sunrise-php/http-message/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/sunrise-php/http-message/?branch=master)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/sunrise-php/http-message/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/sunrise-php/http-message/?branch=master)
[![Total Downloads](https://poser.pugx.org/sunrise/http-message/downloads?format=flat)](https://packagist.org/packages/sunrise/http-message)
[![Latest Stable Version](https://poser.pugx.org/sunrise/http-message/v/stable?format=flat)](https://packagist.org/packages/sunrise/http-message)
[![License](https://poser.pugx.org/sunrise/http-message/license?format=flat)](https://packagist.org/packages/sunrise/http-message)---
## Installation
```bash
composer require sunrise/http-message
```## How to Use
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.
### Server Request from Global Environment
```php
$request = \Sunrise\Http\Message\ServerRequestFactory::fromGlobals();
```### Typed Messages
#### JSON Request
```php
$request = new \Sunrise\Http\Message\Request\JsonRequest('POST', '/', ['foo' => 'bar']);
```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:
```php
$request = new \Sunrise\Http\Message\Request\JsonRequest('POST', '/', [], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE, 512);
```#### URL Encoded Request
```php
$request = new \Sunrise\Http\Message\Request\UrlEncodedRequest('POST', '/', ['foo' => 'bar']);
```You can also specify the [encoding type](https://www.php.net/manual/ru/url.constants.php#constant.php-query-rfc1738) as shown below:
```php
$rfc1738 = \Sunrise\Http\Message\Request\UrlEncodedRequest::ENCODING_TYPE_RFC1738;
$request = new \Sunrise\Http\Message\Request\UrlEncodedRequest('POST', '/', [], $rfc1738);
``````php
$rfc3986 = \Sunrise\Http\Message\Request\UrlEncodedRequest::ENCODING_TYPE_RFC3986;
$request = new \Sunrise\Http\Message\Request\UrlEncodedRequest('POST', '/', [], $rfc3986);
```#### JSON Response
```php
$response = new \Sunrise\Http\Message\Response\JsonResponse(200, ['foo' => 'bar']);
```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:
```php
$response = new \Sunrise\Http\Message\Response\JsonResponse(200, [], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE, 512);
```#### HTML Response
```php
$response = new \Sunrise\Http\Message\Response\HtmlResponse(200, 'Welcome!
');
```### Streams
#### File Stream
```php
$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](https://www.php.net/manual/en/wrappers.php.php#wrappers.php.input).
```php
$stream = new \Sunrise\Http\Message\Stream\PhpInputStream();
```#### Memory Stream
More details about this stream can be found on the [official page](https://www.php.net/manual/en/wrappers.php.php#wrappers.php.input).
```php
$stream = new \Sunrise\Http\Message\Stream\PhpMemoryStream('r+b');
```#### Temporary Stream
More details about this stream can be found on the [official page](https://www.php.net/manual/en/wrappers.php.php#wrappers.php.input).
```php
$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.
```php
$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](https://www.php.net/manual/en/function.tmpfile).
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.
```php
$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:
```php
$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:
```php
try {
} catch (\Sunrise\Http\Message\Exception\ExceptionInterface $e) {
}
```---
## Test Run
```bash
composer test
```## Useful Links
- https://tools.ietf.org/html/rfc7230
- https://www.php-fig.org/psr/psr-7/
- https://www.php-fig.org/psr/psr-17/