📡 Emitting psr-7 responses.
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
build/travis
src avoid use of multi-byte string functions (#17) Oct 30, 2018
tests avoid use of multi-byte string functions (#17) Oct 30, 2018
.changelog added a changelog file (#19) Oct 31, 2018
.editorconfig Working on http emitter Apr 3, 2017
.gitattributes Working on http emitter Apr 3, 2017
.gitignore fixes #7 (#10) Jun 6, 2018
.php_cs
.styleci.yml fixes #1 | added SapiStreamEmitter (#3) May 6, 2017
.travis.yml
CHANGELOG.md added a changelog file (#19) Oct 31, 2018
CODE_OF_CONDUCT.md update email Apr 4, 2017
CONTRIBUTING.md Working on http emitter Apr 3, 2017
LICENSE Initial commit Apr 3, 2017
README.md fixes #9 (#11) Jun 6, 2018
composer.json upgraded narrowspark/coding-standard v1.4.0 (#18) Oct 30, 2018
phpstan.neon Fixed the cs and phpstan errors (#16) Oct 28, 2018
phpunit.xml.dist fixes #9 (#11) Jun 6, 2018

README.md

Http Response Emitter

Emits a Response to the PHP Server API.

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