嗯,来自网络浏览器的奇怪行为.我正在使用此代码:
ob_end_flush();
ini_set("output_buffering", "0");
ob_implicit_flush(true);
function pingtest()
{
$proc = popen("ping -c 5 google.com", 'r');
while (!feof($proc))
{
echo "[".date("i:s")."] ".fread($proc, 4096);
}
}
?>
Immediate output:
pingtest();
?>
在浏览器中,在收到所有字节后显示内容.
但是,内容实际上是按时交付的,请做这个测试:
wget -O - -q "http://localhost/ping.php"
您将看到响应由php& apache2准时.
我暂时在长期任务中使用这种执行,但使用更复杂的解决方案:
>接口的html文件
>运行长任务的php文件
>使用EventSource对象连接html界面和php长执行(在html5上可用)
接口(test.html)
Simple EventSource examplefunction eventsourcetest() {
var ta = document.getElementById('output');
var source = new EventSource('test.php');
source.addEventListener('message', function(e) {
if (e.data !== '') {
ta.value += e.data + '\n';
}
}, false);
source.addEventListener('error', function(e) {
source.close();
}, false);
}
Output:
ping google.com
服务器端组件(test.php)
ob_end_flush();
ini_set("output_buffering", "0");
ob_implicit_flush(true);
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
function echoEvent($datatext) {
echo "data: ".implode("\ndata: ", explode("\n", $datatext))."\n\n";
}
echoEvent("Start!");
$proc = popen("ping -c 5 google.com", 'r');
while (!feof($proc)) {
echoEvent(fread($proc, 4096));
}
echoEvent("Finish!");
将两个文件放在Web服务器上的一个位置并输入test.html,我认为这是您从一开始就要查找的内容.