How do you run a long PHP script and keep sending updates to the browser via HTTP?(你如何运行一个很长的 PHP 脚本并通过 HTTP 不断地向浏览器发送更新?)
问题描述
如何运行一个很长的 PHP 脚本并通过 HTTP 不断向浏览器发送更新?
How do you run a long PHP script and keep sending updates to the browser via HTTP?
与输出缓冲有关,但我不知道具体如何.
Something to do with output buffering but I don't know exactly how.
推荐答案
输出缓冲正在朝着正确的方向思考,您可以使用 ob_start() 开始输出缓冲,就像使用会话一样 (session_start) 位于脚本顶部的某个位置,然后发送任何输出.
Output Buffering is thinking in the right direction, you start output buffering with ob_start() just like you would with sessions (session_start) somewhere in the top of your script, before any output is sent.
然后,您可以使用 ob_flush 和 flush 继续刷新输出.例如,如果您在 foreach 循环中,并且在每个循环结束时您想输出新行并等待 1 秒,您可以这样做.
Then, you can use ob_flush and flush to keep flushing the output. For example, if you are in a foreach loop and at the end of each loop you want to output the new row and wait 1 second you would can do that. 
但还要查看 set_time_limit,否则人们可能会在 30 秒左右后遇到超时.
But also look at set_time_limit, because otherwise people might experience a timeout after 30 seconds or so. 
另一个快速说明,某些浏览器在实际开始显示之前需要最少的输出字节数.我不确定它是多少字节,我认为它在 4000 左右.此外,某些浏览器在关闭之前不会呈现某些元素(如表格).所以冲洗在那里也不起作用.
Another quick note, some browsers require a minimum number of bytes of output before they actually start showing it. I'm not sure what amound of bytes it was, I think it was around the 4000. Also, some browsers won't render certain elements (like tables) until they are closed. So flushing won't work there either.
这篇关于你如何运行一个很长的 PHP 脚本并通过 HTTP 不断地向浏览器发送更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:你如何运行一个很长的 PHP 脚本并通过 HTTP 不断地向浏览器发送更新?
				
        
 
            
        基础教程推荐
- 使用 scandir() 在目录中查找文件夹 (PHP) 2022-01-01
 - Web 服务器如何处理请求? 2021-01-01
 - Yii2 - 在运行时设置邮件传输参数 2022-01-01
 - php中的PDF导出 2022-01-01
 - php中的foreach复选框POST 2021-01-01
 - php 7.4 在写入变量中的 Twig 问题 2022-01-01
 - PHPUnit 的 Selenium 2 文档到底在哪里? 2022-01-01
 - 如何在数学上评估像“2-1"这样的字符串?产生“1"? 2022-01-01
 - 主题化 Drupal 7 的 Ubercart “/cart"页 2021-01-01
 - 将变量从树枝传递给 js 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
				
				
				
				