强制 PHP 桌面流式传输 php 响应

Forcing PHP Desktop to stream php responses

我遇到的问题是当我循环回显某些内容时需要蒸汽输出。现在通常我会放一些东西,比如

ini_set('output_buffering', 'off');
ini_set('zlib.output_compression', false);

ob_end_flush();
while (@ob_end_flush());

ini_set('implicit_flush', true);
ob_implicit_flush(true);

header("Content-type: text/html");
header('Cache-Control: no-cache');

在页面顶部然后调用

echo 'Something to print out here';
ob_flush();
flush();

然而,这根本不起作用。我没有生成任何错误,也没有显示它只是没有缓冲就没有根据需要立即输出。好像一点作用都没有。我也尝试过更改 php.ini 文件。这也没有效果。我已经在 2 个不同版本的 PHP 桌面上尝试过这个。我已经在使用 PHP 5.4 的 PHP Desktop 47.0 Chrome 和使用 PHP 7 的最新版本 PHP Desktop 57.0 上进行了尝试。将不胜感激。

更新 我收到了 php 桌面开发人员的回复,他不知道为什么它不工作,并建议 php 桌面使用的 Mongoose Web 服务器可能不支持这个。有没有人比我更有 Mongoose 经验?除了使用 php desktop

之外,我从未真正使用过它

在 Mongoose 中使输出缓冲区刷新工作的技巧是在调用 ob_flush/flush 之前输出总共 8192 个字符。下面的示例代码,阅读 php 评论和 html 评论了解更多详情。

<?php

error_reporting(-1);

ini_set('zlib.output_compression', 0);
ini_set('output_buffering', 0);
ini_set('implicit_flush', 1);

// This buffer length value is copied from "mongoose.c" file.
// If you would like to reduce buffer size then modify "mongoose.c"
// file and rebuild phpdesktop from sources.
define("MG_BUF_LEN", 8192);

function fprint($s)
{
    $args = func_get_args();
    call_user_func_array('printf', $args);
    print(str_repeat(" ", MG_BUF_LEN));
    @ob_flush();
    flush();
}

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-cache, no-store, must-revalidate");
header("Pragma: no-cache");

?>

<style type="text/css">@import url("style.css");</style>
<a href="index.php">Go back to index</a>
| <a href="<?php echo $_SERVER["REQUEST_URI"];?>">Refresh</a>

<title>Output buffer flush</title>
<h1>Output buffer flush</h1>

<p>
    This example forces flush of output buffer repeatedly.
</p>
<p>
    This technique works differently with Mongoose web server.
    Mongoose forces to always read 8192 characters (MG_BUF_LEN)
    before sending output. The solution is to output 8192 spaces
    before each ob_flush / flush calls. Spaces are ignored in html,
    so this output is not visible. It should really be 8192
    characters minus characters previously outputted for it to
    work always correctly. In this simple examples this is not
    required.
</p>

<?php

fprint("\r\n\r\n");
sleep(1);
fprint("Slept for 1 second<br>");
sleep(2);
fprint("Slept for 2 seconds<br>");
sleep(3);
fprint("Slept for 3 seconds<br>");
fprint("Done.")

?>

我已将 "ob-flush.php" 示例提交到 phpdesktop:

https://github.com/cztomczak/phpdesktop/blob/2a800fecd8830e4f1e6c4054e74e8d03b4900847/phpdesktop-chrome57/www/ob-flush.php