使用Ajax在PHP中运行函数时显示实时调试消息

时间:2022-04-07 20:45:10

I have a function like this that may run for 20 minutes:

我有这样的功能,可能会运行20分钟:

function  test($videoarray)
{
    for($i;$i<100;$i++)
        if(converVideo($videoarray[$i]))
        {
           echo 'video number '.$i.' has been convert';
        }
        else
        {
           echo 'video number '.$i.' Is too large.';
        } 
}

During this time I wish I could see the messages live, for example with Ajax/PHP.

在此期间,我希望能够看到消息,例如使用Ajax / PHP。

Is it possible?

可能吗?

Desired result:

1st min:  video number 1 has been converted.
2nd min:  video number 2 is too large.
,
.
.

1 个解决方案

#1


I would recommend adding flush(); after each echo

我建议添加flush();每次回声后

This should force the message to the screen once written.

这应该在写入后强制将消息发送到屏幕。

Example

echo 'video number '.$i.' has been convert';
flush();

PHP FLUSH

This might not always work therefore one can always persist the messages to a database and have a polling mechanism to read the message every couple of seconds.

这可能并不总是有效,因此可以始终将消息持久保存到数据库并具有轮询机制以每隔几秒读取消息。

EXAMPLE of PHP Polling

PHP轮询的示例

#1


I would recommend adding flush(); after each echo

我建议添加flush();每次回声后

This should force the message to the screen once written.

这应该在写入后强制将消息发送到屏幕。

Example

echo 'video number '.$i.' has been convert';
flush();

PHP FLUSH

This might not always work therefore one can always persist the messages to a database and have a polling mechanism to read the message every couple of seconds.

这可能并不总是有效,因此可以始终将消息持久保存到数据库并具有轮询机制以每隔几秒读取消息。

EXAMPLE of PHP Polling

PHP轮询的示例