How to make PHP generate Chunked response

后端 未结 9 2074
渐次进展
渐次进展 2020-11-28 06:32

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

9条回答
  •  鱼传尺愫
    2020-11-28 07:09

    Andriy F.'s solution worked for me. Here's a simplified version of his answer:

    function dump_chunk($chunk)
    {
        echo $chunk;
        flush();
        ob_flush();
    }
    
    header('Content-Type: text/html; charset=UTF-8');
    
    flush();
    
    for ($i = 0; $i < 3; $i++) {
        dump_chunk('Sending data chunk ' . ($i + 1) . ' of 1000 
    '); sleep(1); }

    Although I don't understand why the ob_flush() call is needed. If someone knows, please comment.

提交回复
热议问题