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_en
The output buffer will not be sent to the browser until it is full. The default size is 4096
bytes. So you either need to change the buffer size or pad your chunks. Also, the browser may have it's own minimum buffer before the page is displayed. Note that according to the Wikipedia Article about chunked encoding, sending a 0\r\n\r\n
chunk terminates the response.
If you want to change the output buffering size setting, I read that you cannot use ini_set('output_buffering', $value)
. Instead, change the output buffering setting by adding the following to your php.ini file.
php_value output_buffering 1024
Here's an example of padding your chunks
header("Transfer-Encoding: chunked");
header("Content-Encoding: none");
// Send chunk to browser
function send_chunk($chunk)
{
// The chunk must fill the output buffer or php won't send it
$chunk = str_pad($chunk, 4096);
printf("%x\r\n%s\r\n", strlen($chunk), $chunk);
flush();
}
// Send your content in chunks
for($i=0; $i<10; $i++)
{
send_chunk("This is Chunk #$i.
\r\n");
usleep(500000);
}
// note that if you send an empty chunk
// the browser won't display additional output
echo "0\r\n\r\n";
flush();
Here's a short version that demonstrates 0\r\n\r\n\
terminating the output:
$output = "hello world";
header("Transfer-Encoding: chunked");
header('Content-Encoding: none');
printf("%x\r\n%s\r\n", strlen($output), $output);
ob_flush();
print("0\r\n\r\n");
ob_flush();
flush();
sleep(10);
print('POST PROCESSING');