views:

553

answers:

1

I googled for this problem but there is no answer for it.

I want my PHP script to generate HTTP response in chunked( http://en.wikipedia.org/wiki/Chunked_transfer_encoding). How to do it?

Update: I figured it out. I have to specify Transfer-encoding header and flush it.

header("Transfer-encoding: chunked");
flush(); 

The flush is necessary. Otherwise, Content-Length header will be generated.

And, I have to make chunks by myself. With a helper function, it is not hard.

function dump_chunk($chunk)
{
    echo sprintf("%x\r\n", strlen($chunk));
    echo $chunk;
    echo "\r\n";
}
+1  A: 

You should be able to use:

<?php header("Transfer-Encoding: chunked");

but you'll have to ensure yourself that the output follows the specifications.

andre
It seems not enough. I have to do flush() right after last header() statement to make sure "Content-Length" header is not generated.
Morgan Cheng