【问题标题】:how can I get php real-time browser output我怎样才能获得php实时浏览器输出
【发布时间】:2020-03-16 19:49:43
【问题描述】:

我在这里找到的所有类似问题的答案都是多年前的,对我没有帮助。 我已经尝试了我在网上和 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】:

    您需要查看端点的前端轮询或通过 websockets 之类的东西设置流。简单的解释是浏览器等待来自您的网络服务器的响应,直到 php 脚本完成后才完成。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-04
      • 2014-08-22
      • 1970-01-01
      • 2020-10-07
      • 2017-05-25
      • 1970-01-01
      • 2014-05-19
      相关资源
      最近更新 更多