doubeishuai6598 2015-09-15 11:26
浏览 105

如何在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));
    }
} 
  • 写回答

2条回答 默认 最新

  • dro60731 2015-09-15 11:38
    关注

    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.

    评论

报告相同问题?