开发者

PHP request "in background"

开发者 https://www.devze.com 2023-03-21 22:01 出处:网络
Is there a way to make PHP request where user doesn\'t have to wait for response? Some sort of \"php request in background\"?

Is there a way to make PHP request where user doesn't have to wait for response? Some sort of "php request in background"?

For example, if application needs to send 100 emails because the user had submitted something, I don't want to开发者_如何学编程 show "sending... please wait" for this user, but I want some other script to do the job independent from that user...


Options:

  1. Stick in a db (or a file) and use a cron to poll it (easiest as you probably already use a db).
  2. Use something like RabbitMQ or ØMQ (my favourite)
  3. Spawn a separate process to do it using fork/exec (would not recommend that).
  4. As others have suggested - fake it by using an Ajax request. Viable - but I find it ugly.


You can maybe send the request in Ajax this way to UI won't freeze and the task will be executed on the server?


You could send the request via ajax and then redirect the user elsewhere upon success. The server script will still process, but no confirmation will be given to the user.


exec('php script.php > /dev/null & echo $!', $o);

You may want to use php-cli instead of php as well. The command above returns the process id of the background script in $o[0] so you can set something up to poll it with ajax or somethig if you want to show the user it's progress.


Create a php routine to do the actual work of sending the emails and call it via http GET using the technique described here.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号