继续执行脚本而不等待在php中返回

时间:2015-05-06 10:41:28

标签: php pdf phpmailer delayed-execution

我有一个应用程序,我在客户端检索的数据上创建/生成PDF。生成PDF时,我将这些PDF发送到所需的电子邮件并创建下载按钮,因此客户也可以下载该文件。 问题是在我创建下载按钮之前发送邮件大约需要10-15秒,甚至更多。 有没有办法我可以在后台放置电子邮件发送过程,所以我可以直接跳到代码的下载部分,而不是等待邮件程序做所有的技巧。

$name = md5(time());
$pdfoutput = $in->_config["path"] . '/PDFs/' . $name . '.pdf';
$mpdf->Output( $pdfoutput,'F' );
$subject = "Offer";
$body["html"] = "<b>Offer in html</b>";
$body["text"] = "Offer as text";
$files = array($pdfoutput);
$addresses = array("*******@hotmail.com");

// PROBLEMATIC PART
$mail->createEmails($addresses, $subject, $body, $files); // i want this proces to go in background

$pdfurl = $in->_config["url"] . "PDFs/" . $name . ".pdf";
return json_encode(array("status" => "ok", "reason" => "PDF CREATED!", "pdffile" => $pdfurl));

2 个答案:

答案 0 :(得分:0)

PHP Fatal error: Call to undefined function pcntl_fork() 

这只是意味着你没有安装/启用pcntl扩展。实际上我建议不要使用来自apache SAPI的pcntl - 你可以使用shell_exec从apache SAPI生成一个没有pcntl forfor的PHP实例,并且有一些很好的包装来帮助它{{3 }}

通过数据库排队工作正常,如果您已经在使用数据库,则非常容易。否则,我建议cocurbeanstalkd与之交谈。然后从cron或作为守护进程运行发送过程。

答案 1 :(得分:0)

为什么不将它移动并输出你的json?也冲洗你的缓冲区。

http://php.net/manual/en/function.ignore-user-abort.php

相关问题