Skip to content

PrisisForks/http-emitter

 
 

Repository files navigation

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

About

📡 Emitting psr-7 responses.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 99.0%
  • Shell 1.0%