Jak*_*ith 9 php buffer output-buffering
我已经尝试了几次尝试让我的flush和ob_flush工作.我已经尝试设置ini以允许缓冲,我尝试使用我在网上找到的几个不同的功能来进行输出缓冲,而且根本没有任何功能正常工作.该脚本希望等到它完成后直到它回显输出.这是我到目前为止的脚本
ob_start();
//Login User
echo 'Logging in to user<br>';
ob_flush();
flush();
$ch = curl_init("http://www.mysite.com/login/");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "username=$user&pass=$pass");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies/$cookie");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies/$cookie");
$output = curl_exec($ch);
curl_close($ch);
ob_flush();
flush();
//Update Status
echo 'Updating Status<br>';
ob_flush();
flush();
$ch = curl_init("http://www.mysite.com/update/");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "status=$status");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies/$cookie");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies/$cookie");
$output = curl_exec($ch);
curl_close($ch);
ob_flush();
flush();
Run Code Online (Sandbox Code Playgroud)
我希望它回应它正在做的事情,然后运行该函数,然后回显其他东西,然后做另一个函数.我希望在浏览器上实时刷新和回显所有缓冲区.
net*_*der 42
这里的想法是禁用输出缓冲,而不是启用它.正如其名称所示,输出缓冲将输出保存到内存并在脚本末尾显示,或者在明确要求时显示.
话虽如此,您不必为每个输出显式刷新.在显示任何输出之前使用以下内容,然后每次回显时都不必费心冲洗:
ob_implicit_flush(true);
ob_end_flush();
Run Code Online (Sandbox Code Playgroud)
每个例子:
ob_implicit_flush(true);
ob_end_flush();
for ($i=0; $i<5; $i++) {
echo $i.'<br>';
sleep(1);
}
Run Code Online (Sandbox Code Playgroud)
将输出0到4,每秒显示一次.
我只是想快速记录一下我在 2016 年观察到的不同方法的建议:
netcoder 和David提供的上述代码在以下浏览器中对我有用:
它似乎不适用于 Firefox、Safari 或 IE 10-11。
我还测试了替代代码:
<?php
if (ob_get_level() == 0) ob_start();
for ($i = 0; $i<10; $i++){
echo "<br> Line to show.";
echo str_pad('',4096)."\n";
ob_flush();
flush();
sleep(2);
}
echo "Done.";
ob_end_flush();
?>
Run Code Online (Sandbox Code Playgroud)
可以在这里找到:http : //php.net/manual/en/function.flush.php#54841
通过所有浏览器似乎有更好的当前支持:
工作实现似乎每年都在变化,所以我想提供我目前发现自己工作的更新。