composer require sunrise/http-message
We highly recommend studying PSR-7 and PSR-17, as only basic examples are provided below.
$request = \Sunrise\Http\Message\ServerRequestFactory::fromGlobals();
$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);
$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);
$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);
$response = new \Sunrise\Http\Message\Response\HtmlResponse(200, '<h1>Welcome!</h1>');
$stream = new \Sunrise\Http\Message\Stream\FileStream('/folder/file', 'r+b');
More details about this stream can be found on the official page.
$stream = new \Sunrise\Http\Message\Stream\PhpInputStream();
More details about this stream can be found on the official page.
$stream = new \Sunrise\Http\Message\Stream\PhpMemoryStream('r+b');
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);
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
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
Any exceptions thrown by this package can be caught through the following interface:
try {
} catch (\Sunrise\Http\Message\Exception\ExceptionInterface $e) {
}
composer test