Swoole\Http\Response->trailer()

4.x is outdated, please check the latest version 22.x


Latest version: pecl install openswoole-22.1.2

Declaration

<?php Swoole\Http\Response->trailer(string $key, string $value, bool $format = true)

Parameters

key

The key of HTTP response header

value

The value of HTTP response header

format

If true, the header key will be automatically formatted to fit HTTP naming requirements

Return

successful

No value is returned

fails

If setting the header fails, then false will be returned

Description

Send a HTTP trailer response header to the HTTP client. A trailer header is appended to the HTTP body. This can be used for both HTTP 1.1 and HTTP 2 responses but support for message integrity checks and digital signatures only with HTTP2, which can be enabled with Swoole.

The HTTP trailer header allows the server to include additional field at the end of chunked messages in order to supply metadata that might be dynamically generated while the message body is sent, such as a message integrity check, digital signature, or post-processing status.

Repeat usage of this method will overwrite any previously set keys and their values.

Example

Server request using the trailer header method:

<?php

$server->on('Request', function(Swoole\Http\Request $request, Swoole\Http\Response $response)
{
  $response->trailer('grpc-status', 0);

  $response->end();
});

$server->on('Request', function(Swoole\Http\Request $request, Swoole\Http\Response $response)
{
  $data = 'Hello World!';
  $response->write($data);

  $response->trailer('Grpc-Status', 0);
  $response->trailer('Grpc-Status', '');

  $response->trailer('Content-MD5', md5($data));

  $response->end();
});

Output from example to client:

HTTP/1.1 200 OK
Content-Type: text/plain
Transfer-Encoding: chunked
Trailer: Expires

7\r\n
Swoole.co.uk\r\n
9\r\n
Developer\r\n
0\r\n
Content-MD5: 0800fc577294c34e0b28ad2839435945\r\n
\r\n
Last updated on August 31, 2022