如何正确刷新 PHP echo 输出

huangapple go评论53阅读模式
英文:

How to properly flush PHP echo output

问题

I am using PHPMailer to send emails with attachments.

Each email takes about 5 seconds to send, and the PHP can send up to 5 emails with a total in that case of 10 PDFs...

During all that time, the browser shows a white page.

<?php
echo '<div>some spinner html</div>';
// code here to send emails
if (!$mail->send()) {
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message sent!';
}

echo 'all the rest of the html';
?>

This doesn't work...
I have the feeling I can use an ob_start() to buffer the rest of the page and only show a spinner.

英文:

I am using PHPMailer to send emails with attachments.

Each email takes about 5 seconds to send, and the PHP can send up to 5 emails with a total in that case of 10 PDFs...

During all that time, the browser shows a white page.

<?php
echo '<div>some spinner html</div>';
// code here to send emails
if (!$mail->send()) {
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message sent!';
}

echo 'all the rest of the html';
?>

This doesn't work...
I have the feeling I can use an ob_start() to buffer the rest of the page and only show a spinner.

答案1

得分: 2

Real answer: 使用 JS 并使用 PHP 创建 API

解决您的问题的最佳方案是使用 JavaScriptXHR,为其创建一个漂亮的用户界面。如果您想在发送电子邮件后显示加载,然后在加载消失后显示其他内容,这只能通过 JS 实现。PHP 只是一种服务器端语言,能够向浏览器发送响应,无法访问已呈现的页面!

刷新输出

只需使用 ob_flush()flush() 函数:

echo '<div>some spinner html</div>';
ob_flush();
flush();
// 这里是发送电子邮件的代码
if (!$mail->send()) {
    echo 'Mailer Error: ' . $mail->ErrorInfo;
    ob_flush();
    flush();
} else {
    echo 'Message sent!';
    ob_flush();
    flush();
}

echo '所有其他的 HTML';

使用隐式刷新

这看起来像上一个例子,但减少了一个函数调用。这去掉了 flush() 调用!

ob_implicit_flush(true);
echo '<div>some spinner html</div>';
ob_flush();
// 这里是发送电子邮件的代码
if (!$mail->send()) {
    echo 'Mailer Error: ' . $mail->ErrorInfo;
    ob_flush();
} else {
    echo 'Message sent!';
    ob_flush();
}

echo '所有其他的 HTML';

关闭输出缓冲区

另一种解决方案是关闭输出缓冲区:

while (@ob_end_flush());
echo '<div>some spinner html</div>';
// 这里是发送电子邮件的代码
if (!$mail->send()) {
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message sent!';
}

echo '所有其他的 HTML';
英文:

Real answer: Use JS and create an API using PHP

Best solution to your problem is using JavaScript and XHR and make a beautiful UI for it. If you want to show a loading and remove it after emails sent and show other stuff after disappearance of loading, it's only possible through JS. PHP is just a server side language capable of sending a response to browser and have no access to rendered page!

Flush output

Just use ob_flush() and flush() functions:

echo &#39;&lt;div&gt;some spinner html&lt;/div&gt;&#39;;
ob_flush();
flush();
// code here to send emails
if (!$mail-&gt;send()) {
    echo &#39;Mailer Error: &#39; . $mail-&gt;ErrorInfo;
    ob_flush();
    flush();
} else {
    echo &#39;Message sent!&#39;;
    ob_flush();
    flush();
}

echo &#39;all the rest of the html&#39;;

Using implicit flush

This looks like the previous one, but reduced a function call. this removes the flush() call!

ob_implicit_flush(true)
echo &#39;&lt;div&gt;some spinner html&lt;/div&gt;&#39;;
ob_flush();
// code here to send emails
if (!$mail-&gt;send()) {
    echo &#39;Mailer Error: &#39; . $mail-&gt;ErrorInfo;
    ob_flush();
} else {
    echo &#39;Message sent!&#39;;
    ob_flush();
}

echo &#39;all the rest of the html&#39;;

Turn off output buffer

Other solution is to turn off output buffer:

while (@ob_end_flush());
echo &#39;&lt;div&gt;some spinner html&lt;/div&gt;&#39;;
// code here to send emails
if (!$mail-&gt;send()) {
    echo &#39;Mailer Error: &#39; . $mail-&gt;ErrorInfo;
} else {
    echo &#39;Message sent!&#39;;
}

echo &#39;all the rest of the html&#39;;

huangapple
  • 本文由 发表于 2023年5月7日 22:40:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76194587.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定