How to generate Chunked response with a Trailer in Apache/PHP?
PHP will send a chunked response by default if headers are sent and no Content-Length
header was specified. If you're familiar with the HTTP spec, this is the only logical thing to do since the client on the other end needs to know when the HTTP message you're sending ends so it can stop reading.
If you want to do this manually, you need to ...
- Send the appropriate headers yourself and call
flush()
- Manually output the chunked HTTP message
So you might do something like the following. The idea is that you need to manually send your own headers and manually chunk your own message. If you simply don't send a Content-Length
header, however, PHP will send a chunked message for you by default.
header("Transfer-encoding: chunked");header("Trailer: X-My-Trailer-Header");flush();echo dechex(strlen($myChunk)) . "\r\n";echo $myChunk;echo "\r\n";flush();echo "0\r\n";flush();echo "X-My-Trailer-Header: some-value\r\n";flush();