编程语言
首页 > 编程语言> > PHP Flush / ob_flush无效

PHP Flush / ob_flush无效

作者:互联网

我已经尝试了几次尝试使我的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();

我希望它回应它正在做的事情,然后运行该函数,然后回显其他东西,然后做另一个函数.我希望在浏览器上实时刷新和回显所有缓冲区.

解决方法:

这里的想法是禁用输出缓冲,而不是启用它.正如其名称所示,输出缓冲将输出保存到内存并在脚本末尾显示,或者在明确要求时显示.

话虽如此,您不必为每个输出显式刷新.在显示任何输出之前使用以下内容,然后每次回显时都不必费心冲洗:

ob_implicit_flush(true);
ob_end_flush();

每个例子:

ob_implicit_flush(true);
ob_end_flush();

for ($i=0; $i<5; $i++) {
   echo $i.'<br>';
   sleep(1);
}

将输出0到4,每秒显示一次.

标签:output-buffering,php,buffer
来源: https://codeday.me/bug/20190917/1809347.html