narrowspark/http-emitter

v0.6.0 2018-06-06 13:56 UTC

README

Emits a Response to the PHP Server API.

68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6e6172726f77737061726b2f687474702d656d69747465722e7376673f7374796c653d666c61742d737175617265 68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253545372e312e302d3838393242462e7376673f7374796c653d666c61742d737175617265 68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f727573742d6c616e672f727573742f6d61737465722e7376673f7374796c653d666c61742d737175617265 68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f6e6172726f77737061726b2f687474702d656d69747465722f6d61737465722e7376673f7374796c653d666c61742d737175617265 68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265

The available emitter implementations are:

- `Narrowspark\HttpEmitter\SapiEmitter`
- `Narrowspark\HttpEmitter\SapiStreamEmitter`.

Note: Each use the native PHP functions header() and echo in order to emit the response.

- `Narrowspark\HttpEmitter\SwooleEmitter`

Note: Swoole is an async programming Framework for PHP that can be used to create high performance HTTP server applications, e.g. web APIs.

If you are using a non-SAPI implementation, you will need to create your own Narrowspark\HttpEmitter\EmitterInterface implementation.

Note: If headers have been sent, or the output buffer exists and has a non-zero length, the emitters raise an exception, as mixed PSR-7 / output buffer content creates a blocking issue.

If you are emitting content via echo, print, var_dump, etc., or not catching PHP errors / exceptions, you will need to either fix your application to always work with a PSR-7 response. Or provide your own emitters that allow mixed output mechanisms.

Installation

composer require narrowspark/http-emitter

Use

How to use the SapiEmitter:

<?php

use Narrowspark\HttpEmitter\SapiEmitter;

$response = new \Response();
$response->getBody()->write("some content\n");

$emitter = new SapiEmitter();
$emitter->emit($response);

How to use the SwooleEmitter:

<?php

use Narrowspark\HttpEmitter\SwooleEmitter;
use Swoole\Http\Server;

$http = new Server('127.0.0.1', 9501);
 
$http->on('start', function ($server) {
    echo 'Swoole http server is started at http://127.0.0.1:9501';
});
 
$http->on("request", function ($request, $response) use ($app) {
    $psr7Response = new \Response();
    $psr7Response->getBody()->write("some content\n");
 
    $emitter = new SwooleEmitter($response);
    $emitter->emit($psr7Response);
});
 
$http->start();

If you missing the Content-Length header you can use the \Narrowspark\HttpEmitter\Util\Util::injectContentLength static method.

<?php

use Narrowspark\HttpEmitter\Util;

$response = new \Response();

$response = Util::injectContentLength($response);

Contributing

If you would like to help take a look at the list of issues and check our Contributing guild.

Note: Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

License

The Narrowspark http-emitter is open-sourced software licensed under the MIT license