domagojk
domagojk

Reputation: 1060

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...

Upvotes: 2

Views: 3045

Answers (5)

Emil Ivanov
Emil Ivanov

Reputation: 37633

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.

Upvotes: 7

Jonathan M
Jonathan M

Reputation: 17441

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

Upvotes: 0

Paul
Paul

Reputation: 141829

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.

Upvotes: 0

Jeff Lambert
Jeff Lambert

Reputation: 24661

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.

Upvotes: 1

Patrick Desjardins
Patrick Desjardins

Reputation: 140753

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

Upvotes: 3

Related Questions