nyholm/psr7

A fast PHP7 implementation of PSR-7

Installs: 199 756

Dependents: 71

Suggesters: 2

Stars: 108

Watchers: 10

Forks: 15

Open Issues: 5

1.0.1 2018-09-02 11:50 UTC

README

Latest Version Build Status Code Coverage Quality Score Total Downloads Monthly Downloads Software License

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

Description Guzzle Zend Slim Nyholm
Lines of code 3 000 3 000 1 700 1 100
PHP7 No No No Yes
PSR-7* 66% 92% 75% 100%
PSR-17 No No No Yes
HTTPlug No No No Yes
Performance** 1.49x 1x 1.17x 1.69x

* Percent of completed tests in https://github.com/php-http/psr7-integration-tests

** See benchmark at https://github.com/Nyholm/http-client-benchmark (higher is better)

Installation

composer require nyholm/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 then those defined in the PSR-7 specification.

Create objects

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

$factory = new \Nyholm\Psr7\Factory\Psr17Factory();
$request = $factory->createRequest('GET', 'http://tnyholm.se');
$stream = $factory->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 \Nyholm\Psr7\Factory\Psr17Factory();
$psr18Client = new Buzz\Client\Curl([], $psr17Factory);

$request = (new Psr17Factory())->createRequest('GET', 'http://tnyholm.se');
$response = $psr18Client->sendRequest($request);

Create server requests

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

composer require nyholm/psr7-server
use Nyholm\Psr7\Factory\Psr17Factory;

$psr17Factory = new Psr17Factory();

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

$serverRequest = $creator->fromGlobals();

Emitting a response

composer require zendframework/zend-httphandlerrunner
$response = (new Psr17Factory())->createReponse('200', 'Hello world');
(new \Zend\HttpHandlerRunner\Emitter\SapiEmitter())->emit($response);