有没有办法让PHP随着脚本的执行逐步输出?
因此,我正在编写一个供我个人使用的一次性脚本,我希望能够了解该过程的进展情况。基本上我正在处理几千个媒体发布并将它们发送到我们的新 CMS。
因此,我不会敲击 CMS,而是在每 5 个请求后让脚本休眠
几秒钟。
我希望 - 在脚本执行时 - 能够看到我的 echo
告诉我脚本将要休眠或者与 Web 服务的最后一个事务已成功。
这在 PHP 中可能吗?
感谢您的帮助!
伊恩
So I'm writing a disposable script for my own personal single use and I want to be able see how the process is going. Basically I'm processing a couple of thousand media releases and sending them to our new CMS.
So I don't hammer the CMS, I'm making the script sleep
for a couple of seconds after every 5 requests.
I would like - as the script is executing - to be able to see my echo
s telling me the script is going to sleep or that the last transaction with the webservice was successful.
Is this possible in PHP?
Thanks for your help!
Iain
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可能想探索刷新命令 http://php.net/manual/en/ function.flush.php
You may want to explore the flush commands http://php.net/manual/en/function.flush.php
使用ob_flush发送缓冲区中的任何数据。因此,您可以执行一些命令,刷新输出,然后在处理更多命令之前休眠一会儿。
默认情况下,PHP 永远不会回显任何内容,直到整个脚本完成执行。这只是您的浏览器试图一次输出太多数据,或者您有某些东西导致它在某处刷新。
Use
ob_flush
to send any data in the buffer. So you can execute some commands, flush the output, and then sleep for a bit before processing more commands.By default, PHP will never echo anything until the entire script finishes executing. That's just your browser trying to output way too much data at once, or you have something that's causing it to flush somewhere.
这是一个将其包装起来的 PHP 包:
https://packagist.org/packages/coderofsalvation/browser -流
Here's a PHP package which wraps it up :
https://packagist.org/packages/coderofsalvation/browser-stream
您可以尝试使用 flush 并查看其他 输出控制函数,但它们可能没有任何用处。不管怎样,您的网络服务器软件可能会缓冲响应。
You can try using flush and look at the other output control functions, but they might not be any use. Your web server software may buffer the response regardless.