httpsoft/http-emitter

PSR-7 响应实现发射

1.1.0 2023-05-05 20:18 UTC

This package is auto-updated.

Last update: 2024-09-05 23:28:32 UTC


README

License Latest Stable Version Total Downloads GitHub Build Status GitHub Static Analysis Status Scrutinizer Code Coverage Scrutinizer Code Quality

此软件包从 Psr\Http\Message\ResponseInterfacePSR-7 HTTP Message 发射 Psr\Http\Message\ResponseInterface 实现。

文档

安装

此软件包需要 PHP 版本 7.4 或更高。

composer require httpsoft/http-emitter

SapiEmitter 使用方法

use HttpSoft\Emitter\SapiEmitter;
use Psr\Http\Message\ResponseInterface;

/** @var ResponseInterface $response */
$response->getBody()->write('Content');

$emitter = new SapiEmitter();
$emitter->emit($response);
// Output result: 'Content'

默认情况下,将发射响应的全部内容。要分部分发射内容,需要指定最大缓冲区长度

$emitter = new SapiEmitter(8192);
$emitter->emit($response);
// Output result: 'Content'

使用 Content-Range 头只发射部分内容

$emitter = new SapiEmitter(8192);
$emitter->emit($response->withHeader('Content-Range', 'bytes 0-3/7'));
// Output result: 'Cont'

要只发射状态行和头部而没有主体,需要将第二个参数指定为 true

$emitter = new SapiEmitter(8192);
$emitter->emit($response, true);
// Output result: ''