回声消息,而php脚本仍然执行 [英] Echo messages while php script still executes

查看:139
本文介绍了回声消息,而php脚本仍然执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用cURL的php脚本,需要大约10-15分钟才能执行。它做了什么,它解析大约1000页寻找特定的匹配,并在整个脚本我有诊断消息回声,像去下一页,找到匹配,加载页错误... ...它的工作方式现在(和正常的方式)是执行了10分钟,然后吐出所有我的自定义消息。
我想能够显示这些消息,当它们发生,而不是当脚本完成执行时。我在想像AJAX会做到,但不知道如何工作。任何提示都非常感谢。谢谢。

I have a php script that uses cURL and takes about 10-15 minutes to execute. What it does, it parses about 1000 pages looking for specific matches and throughout the script I have diagnostic messages echo'ed out, like "Going to the next page", "Found a match", "Error loading page" ... The way it works now (and the way that it's normal) is it executes for like 10 minutes and only then spits out all my custom messages. I would like to be able to display those messages as they happen, not when the script is done executing. I was thinking something like AJAX would do it, but am not sure how it would work. Any tips are greatly appreciated. Thanks.

推荐答案

所以,这是一个老帖子,但我发现了一个解决方案。因为我也必须做同样的事情,当脚本仍然运行时输出。没有任何答案从这里帮助。
首先,我使用Win32服务器(生产)和XAMPP作为本地测试。这个例子只是一个概念证明,可以随意修改。

So, this is a old post but I found a solution for this. As I also have to make the same thing, output when the script is still running. Not any answer from here helped. First of all, I am using Win32 server(production) and XAMPP as local for tests. This example is just a proof of concept and can be modified as you please.

<?php 
ob_implicit_flush(true);
for($i=1; $i<=10; $i++){
echo "$i ...<br>";
for($k = 0; $k < 40000; $k++) echo ' ';
sleep(1);
}
?>

因此,我们打开输出缓冲区。然后我们做一个演示循环从1到10计数,并显示它们被处理的值。第二个循环将填充浏览器缓冲区。最后检查一切是否正常工作,我们睡1秒钟。否则脚本将运行得太快,我们不知道我们是否实现了目标。
希望这有帮助!

So, we open output buffer as implicit. Then we make a demo loop to count from 1 to 10 and display the values as they are been processed. Second loop will fill in the browsers buffer. And finally to check if everything is working well we make a sleep for 1 second. Otherwise the script will run too fast and we could not know if we achieved the goal. Hope this helps !

这篇关于回声消息,而php脚本仍然执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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