Ok so forgive me if this is a repeat of another question, but after searching I have not found a clear answer. What I basically want to do is have my php web application fire off some event (like an emailer or report generator) that may take a few minutes to complete and immediately return control to the page. I come from a .NET world where this can easily be accomplished with a thread.
So here's the workflow:
What's the best way to accomplish this? Brief answers are fine. I don't want code written for me, just some guidance. I looked at shell_exec but I'm not sure exactly if that is the best way or if it is, how to use it to process functions within a web app. (I'm using the Yii framework if that makes any difference). Thanks.
-Jason
The best (and only AFAIK) way to start a new thread thread like thing in PHP is to create a new PHP request using something like curl.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.yoursite.com/background-script.php');
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 1);
curl_exec($ch);
curl_close($ch);
This will run the script, and immediately return the page. Of course there's no callback, so your best bet is to put the output of background-script.php into a database, and then ping the database every so often from the client until you see the results.
We've created a function here that returns an answer to the browser and keeps executing. We use it to send email without making the user wait for it.
public function redirectAndContinue( $url )
{
ignore_user_abort(true);
header("Location: $url");
header("Connection: close");
header("Content-Length: 0");
flush();
}
You may use it like this
// do quick basic stuff to validate your report
$this->redirectAndContinue( $url )
// send your email or do other slow stuff
The problem is: it doesn't work with AJAX, it must be a regular page request (possibly with a POST). But you can make the progress bar anyway: save something in your database or session and then keep calling some URL to get the progress via AJAX.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With