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:
- Stick in a db (or a file) and use a cron to poll it (easiest as you probably already use a db).
- Use something like RabbitMQ or ØMQ (my favourite)
- Spawn a separate process to do it using fork/exec (would not recommend that).
- 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.
精彩评论