PHP code example of sunrise / http-message

1. Go to this page and download the library: Download sunrise/http-message library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sunrise / http-message example snippets


use Sunrise\Http\Message\ServerRequestFactory;

$request = ServerRequestFactory::fromGlobals();

use Sunrise\Http\Message\Response\HtmlResponse;

/** @var $html string|Stringable */

$response = new HtmlResponse(200, $html);

use Sunrise\Http\Message\Response\JsonResponse;

/** @var $data mixed */

$response = new JsonResponse(200, $data);

$response = new JsonResponse(200, $data, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE, 512);

use Sunrise\Http\Message\Stream\FileStream;

$fileStream = new FileStream('/folder/file', 'r+b');

use Sunrise\Http\Message\Stream\PhpInputStream;

$inputStream = new PhpInputStream();

use Sunrise\Http\Message\Stream\PhpMemoryStream;

$memoryStream = new PhpMemoryStream('r+b');

use Sunrise\Http\Message\Stream\PhpTempStream;

$tempStream = new PhpTempStream('r+b');

$maxMemory = 1e+6; // 1MB

$tempStream = new PhpTempStream('r+b', $maxMemory);

use Sunrise\Http\Message\Stream\TmpfileStream;

$tmpfileStream = new TmpfileStream();

// Returns the file path...
$tmpfileStream->getMetadata('uri');

use Sunrise\Http\Message\Stream\TempFileStream;

$tempFileStream = new TempFileStream();

// Returns the file path...
$tempFileStream->getMetadata('uri');

use Sunrise\Http\Message\Exception\ExceptionInterface;

try {
    // some code...
} catch (ExceptionInterface $e) {
    // some logic...
}