Coder Social home page Coder Social logo

psr7's Introduction

PSR-7 implementation

Latest Version Total Downloads Monthly Downloads Software License Static analysis Tests

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

Description Guzzle Laminas Slim Nyholm
Lines of code 3.300 3.100 1.900 1.000
PSR-7* 66% 100% 75% 100%
PSR-17 No Yes Yes Yes
HTTPlug No No No Yes
Performance (runs per second)** 14.553 14.703 13.416 17.734

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

** Benchmark with 50.000 runs. See https://github.com/devanych/psr-http-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 than those defined in the PSR-7 specification.

Create objects

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

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

$request = $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
$psr17Factory = new \Nyholm\Psr7\Factory\Psr17Factory();

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

$serverRequest = $creator->fromGlobals();

Emitting a response

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

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

Our goal

This package is currently maintained by Tobias Nyholm and Martijn van der Ven. They have decided that the goal of this library should be to provide a super strict implementation of PSR-7 that is blazing fast.

The package will never include any extra features nor helper methods. All our classes and functions exist because they are required to fulfill the PSR-7 specification.

psr7's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.