PHP - 如何在执行脚本时获得部分输出? [英] PHP - How to get partial output when script is executing?

查看:85
本文介绍了PHP - 如何在执行脚本时获得部分输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本可以在更长的时间内执行多个操作。在标准情况下,PHP脚本的输出在完全执行此脚本后发送。如果我的脚本加载很长时间没有任何响应,用户可能会失望。

I have a script which do multiple actions in longer time. In standard situation output from PHP script is sent after full execution of this script. User may be dissapointed if my script will load very long time without any response.

假设我在这个脚本中有一个循环,它将工作10次。
每次执行后我都希望输出到浏览器元素X已完成。是时间。。 X和Y的值将会改变。

Let's say I have in this script a loop which will be working 10 times. After each one execution I want to get output to browser "Element X finished. Y time.". Value of X and Y will be changing.

有没有办法达到这个效果?也许从这个脚本到新脚本的某种调用,通过AJAX将数据发送到浏览器。

Is there any way to reach that effect? Maybe with some kind of call from this script to new script which will sent data to browser throug AJAX.

或者有没有办法只用PHP实现达到这个效果?

Or is there any way to reach this effect only with PHP implementation?

我知道我必须澄清此事。我希望此信息在页面上动态显示。我已经尝试了输出缓冲区功能,但它仍然无法正常工作。完全执行脚本后,将显示浏览器中的输出。测试代码:

I see that I have to clarify the matter. I want this information to appear dynamically on the page. I tried with output buffer functions already, and it still don't work as I need it. Output in browser is showing after full execute of script. Test code:

for ($i = 0; $i <= 10; $i++) {
    ob_start();

    echo "Output " . $i . "<br />";

    ob_flush(); //also only flush() etc.


    sleep(1);
}


推荐答案

是的,应该可以实现仅使用PHP。我记得看到一些与此相关的代码,当我在一段时间内通过wordpress进行追踪时,当它更新模块时,它必须ftp,解压缩,复制文件等,这需要很长时间,并且它喜欢让用户更新它正在做什么......在检查了他们的send_message()函数,并阅读了PHP ob_flush ()页面,我想你想要:

Yes, it should be achieveable with PHP alone. I remember seeing some code related to this when I was tracing through wordpress a while back, when it updates modules, it has to ftp, unzip, copy files, etc, which take a long time, and it likes to keep the user updated with what it's doing at the moment... after inspecting their send_message() function, and reading the PHP ob_flush() page, i think you want:

echo "stuff\n"; // adding a newline may be important here,
                // a lot of io routines use some variant of get_line()
ob_end_flush(); // to get php's internal buffers out into the operating system
flush(); // to tell the operating system to flush it's buffers to the user.

php手册中的人员也暗示可能有助于明确设置header()其中有mime-type和字符集,因此浏览器会在开始时知道它并且不会等到整个二进制对象变为可用,然后再尝试对它具有什么类型的实体进行decypher。

The people at the php manual also implied that it might help to explicitly set a header() with the mime-type and character set in it, so the browser would know that at the start and wouldn't wait for the entire binary object to become available before attempting to decypher what sort of entity it had.

如果这不起作用,你需要进一步修改你的系统php.ini以关闭输出缓冲和压缩,此时你可以去看看ajax解决方案。

If that doesn't work you'd need to further modify your system php.ini to turn output buffering and compression off, at which point you may as well go look at an ajax solution.

ajax解决方案看起来像:

An ajax solution would look something like:

你的脚本吐出一些html / javascript来点击ajax请求,刷新所有它的缓冲区,并停止与用户有关,然后开始一些操作,或者可能仅仅指示某个大型操作将在某个地方的某个数据库中启动以供cron作业获取。页面上的javascript将有一个计时器循环来轮询ajax端点的状态,直到ajax回复它已完成。 ajax端点将通过查询数据库或检查输出文件等来检查任务的状态,并立即吐出所有已知的并终止,让客户决定何时再次询问。这是一个涉及更多活动部件的更多参与和复杂的活动,但如果值得花时间制作,则为用户提供非常好的最终产品。

Your script spits out some html/javascript to hit an ajax request, flushes all it's buffers, and stops concerning itself with the user, then starts some operation, or perhaps merely indicates that some large operation is to commence in a database somewhere for a cron job to pick up. The javascript on your page would have a timer loop to poll an ajax endpoint for status until the ajax replied that it was complete. The ajax endpoint would check on the status of your task by querying the database, or examining output files, etc, and spit all it knew out at once and terminate, letting the client decide when to ask it if things were done again. It is a much more involved and complicated endevour with more moving parts, but achieves a very nice end product for the user, if it's worth your time to craft.

这篇关于PHP - 如何在执行脚本时获得部分输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆