我如何获得php实时浏览器输出

问题描述 投票:0回答:1

我在这里找到的所有类似问题的所有答案都已经有很多年了,对我没有帮助。我尝试了各种建议,这些建议是我在网上和php手册中找到的,但都给出了相同的结果:在php脚本完成执行之前,浏览器中没有输出。这是我尝试过的最新代码:

<?php
// Turn off output buffering
ini_set('output_buffering', 'off');
// Turn off PHP output compression
ini_set('zlib.output_compression', false);

//Flush (send) the output buffer and turn off output buffering
//ob_end_flush();
while (ob_end_flush());

// Implicitly flush the buffer(s)
ini_set('implicit_flush', true);
ob_implicit_flush(true);

//prevent apache from buffering it for deflate/gzip
header("Content-type: text/plain");
header('Cache-Control: no-cache'); // recommended to prevent caching of event data.

for($i = 0; $i < 1000; $i++)
{
echo ' ';
}

ob_flush();
flush();

/// Now start the program output

echo "Program Output";

ob_flush();
flush();
for( $i = 0 ; $i < 10 ; $i++ )
{
    echo $i . '\n<br>';
    ob_flush();
    flush();
    sleep(1);
}
echo 'End ...<br>';
?>

如何获得浏览器中的实时输出?预先感谢!

php browser real-time-updates
1个回答
1
投票

您需要查看端点的前端轮询或通过websocket之类的流进行设置。简单的解释是,浏览器会等待Web服务器的响应,直到php脚本完成后,响应才会完成。

© www.soinside.com 2019 - 2024. All rights reserved.