Skip to content

HTTP message wrapper for PHP 7.4+, based on RFC-7230, PSR-7, and PSR-17.

License

Notifications You must be signed in to change notification settings

sunrise-php/http-message

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HTTP message wrapper for PHP 7.4+, based on RFC-7230, PSR-7, and PSR-17.

Build Status Code Coverage Scrutinizer Code Quality Total Downloads Latest Stable Version License


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

Useful Links