sunrise/http-message

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


README

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

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

Useful links