【问题标题】:Forcing PHP Desktop to stream php responses强制 PHP Desktop 流式传输 php 响应
【发布时间】:2018-03-14 17:19:40
【问题描述】:

当我在循环中回显某些内容时,我需要蒸汽输出。现在通常我会放一些诸如

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 desktop 开发人员的回复,他不知道为什么它不起作用,并建议 php desktop 使用的 Mongoose Web 服务器可能不支持此功能。有没有人比我对猫鼬有更多的经验?除了使用 php desktop 时,我从未真正使用过它

【问题讨论】:

  • 当你说“立即”时——你的意思是不刷新页面吗?
  • 我应该编辑它,我的意思是立即输出而不缓冲。我不介意页面是否刷新。我只需要无需等待即可输出数据
  • 我不明白,等什么?
  • 你能不能只禁用 php.ini 中的输出缓冲区,它会在脚本运行时回显?
  • 你怎么知道它在缓冲之前没有显示?

标签: phpdesktop


【解决方案1】:

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

<?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

【讨论】:

  • 这并不能解决问题。这只是 php 桌面的另一个“hacky”工作,似乎有很多。在对任何大型脚本进行测试时,问题仍然存在。如果可以调整 mongoose 中的缓冲区大小来解决这个问题,为什么不这样做,而不是建议用户从明显不可用的“来源”调整和重建它。
  • @Micha 如果您不喜欢它,请不要使用它。我不会再帮你了。
  • 你一开始就没有。当我问简单的问题时,你是个混蛋。你真的有一个很好的方法来让人们支持你的项目,这就是为什么我看到你的资金无处可去
猜你喜欢
  • 2013-06-16
  • 1970-01-01
  • 1970-01-01
  • 2018-02-19
  • 2011-12-02
  • 1970-01-01
  • 2012-09-05
  • 2016-01-31
  • 1970-01-01
相关资源
最近更新 更多