A fast PHP7 implementation of PSR-7 (A fork of nyholm/psr7, removing final clause from all classes making them extendable)

dev-master / 1.4.x-dev 2023-01-22 19:20 UTC

This package is auto-updated.

Last update: 2024-12-22 23:55:48 UTC


README

A super lightweight PSR-7 implementation. Very strict and very fast.

Installation

composer require terablaze/psr7

If you are using Symfony Flex then you get all message factories registered as services.

Usage

The PSR-7 objects do not contain any other public methods than those defined in the PSR-7 specification.

Create objects

Use the PSR-17 factory to create requests, streams, URIs etc.

$psr17Factory = new \Terablaze\Psr7\Factory\Psr17Factory();
$request = $psr17Factory->createRequest('GET', 'https://teraboxx.com');
$stream = $psr17Factory->createStream('foobar');

Sending a request

With HTTPlug or any other PSR-18 (HTTP client) you may send requests like:

composer require kriswallsmith/buzz
$psr17Factory = new \Terablaze\Psr7\Factory\Psr17Factory();
$psr18Client = new \Buzz\Client\Curl($psr17Factory);

$request = $psr17Factory->createRequest('GET', 'https://teraboxx.com');
$response = $psr18Client->sendRequest($request);

Create server requests

The terablaze/psr7-server package can be used to create server requests from PHP superglobals.

composer require terablaze/psr7-server
$psr17Factory = new \Terablaze\Psr7\Factory\Psr17Factory();

$creator = new \Terablaze\Psr7Server\ServerRequestCreator(
    $psr17Factory, // ServerRequestFactory
    $psr17Factory, // UriFactory
    $psr17Factory, // UploadedFileFactory
    $psr17Factory  // StreamFactory
);

$serverRequest = $creator->fromGlobals();

Emitting a response

composer require laminas/laminas-httphandlerrunner
$psr17Factory = new \Terablaze\Psr7\Factory\Psr17Factory();

$responseBody = $psr17Factory->createStream('Hello world');
$response = $psr17Factory->createResponse(200)->withBody($responseBody);
(new \Laminas\HttpHandlerRunner\Emitter\SapiEmitter())->emit($response);