phasync
ClientRequest / end

Method

ClientRequest::end

Finish the response. With no head sent, an implicit 200.

Description#

public ClientRequest::end(
    ?array $trailers = null
): void

Trailers need a chunked response: name them in a trailer header, and give them here.

Parameters#

$trailers ?array = null

Errors / Exceptions#

ExceptionWhen
LogicExceptionWhen there is a content-length, or the client is HTTP/1.0, or the response may not have them

Examples#

Example 1Trailers after a chunked body
use Swerve\ClientRequest;
use Swerve\RequestHandler;

$app = new RequestHandler(function (ClientRequest $request) {
    // Trailers: named in the head, sent after a chunked body, for what is known only at the end
    $request->sendResponseHeaders(200, ['content-type' => 'text/plain', 'trailer' => 'x-checksum']);
    $hash = hash_init('crc32b');
    foreach (['one ', 'two ', 'three'] as $part) {
        $request->write($part);
        hash_update($hash, $part);
    }
    $request->end(['x-checksum' => hash_final($hash)]);
});

if (debug_backtrace()) {
    return $app;   // swerve loads this file: it is the application's swerve.php
}

// Run directly: serve the application with swerve, and send it requests
require __DIR__ . '/../serve.php';
serve(__FILE__, ["GET / HTTP/1.1\r\nHost: example\r\nTE: trailers\r\n\r\n"]);
Output
HTTP/1.1 200 OK
content-type: text/plain
trailer: x-checksum
Server: Swerve
Transfer-Encoding: chunked
Connection: close

4
one 
4
two 
5
three
0
x-checksum: 7825d3d7