How to call a function asynchronously in PHP?

How to call a function asynchronously in php

  • I want to execute these functions when user click on button but I want to execute $model->user->email ? $model->sendUserMail(): ""; that function asynchronously in background while other function will execute as click occur Here is my code if (isset($_POST['create_send'])) { if ($model->save()) { $model->order_id = strtotime(date("Y-m-d H:i:s")) + '' + $model->id; // send mail $model->sendMail(); //send sms $messages = $model->sendSMSMessage($model); // create booking history $model->createBookingHistory($model , $messages); $model->user->email ? $model->sendUserMail(): ""; if($model->booking_stage != 'inprogress'){ $this->sendPushToSeller($model); } $model->update(); $this->redirect(array('index', 'id' => $model->id)); } }

  • Answer:

    There is nothing like Async Tasks in Standard PHP. You could install pthreads or you have to work with some sort of a Queue. With a Queue it would work like that: Create a Mail-Send-Job Push that Job into a Queue (for example a Database) A third party programm (for example a cronjob) has to poll the Queue and execute the Tasks (send the Mail) if there are some Or like @Alan Machado said, use AJAX. So your page can load, and the Buttons sends an AJAX Request to the Server that sends the Mail.

Himanshu Gupta at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

You tagged it yii, so perhaps consider a RESTful API extention, and research AJAX and CRUD. It depends on how far you want to take it. But I feel a RESTful API with yii is the best way to go when it comes to AJAX.

turbopipp

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.