我正在开发一个项目,有两个过程。
我需要做所有这一切,但是要花很多时间。所以现在我正在考虑使用多线程。 谁能帮助您在php中实现这一目标?
我只需要尽快发送回复。 这是我的代码 我设置了一个发送邮件的功能,并且在成功发送完整邮件之后,我使用JSON编码向用户发送响应。
/ 邮件功能 /
public function emailSent($booking_id, $mailHeading, $smallHeading, $subject, $smallHeadingcustomer ,$status, $senttoestimator = true, $senttocustomer = true, $mailSent = false){
if(!empty($booking_id)){
$bookingData = $this->Cnc_model->getData('bookings', array('*'), array('id'=>$booking_id));
if(!empty($bookingData)){
if(!empty($bookingData[0]['estimator_id']) && $bookingData[0]['estimator_id'] != 0){
$name = $this->Cnc_model->getData('users',array('username', 'email'), array('user_id'=> $bookingData[0]['estimator_id']));
$estimator_name = $name[0]['username'];
$estimator_mail = $name[0]['email'];
}
if($bookingData[0]['customer_id'] != 0){
$cname = $this->Cnc_model->getData('customers',array('name', 'office_contact_email'), array('id'=> $bookingData[0]['customer_id']));
$customer_name = $cname[0]['name'];
$customer_mail = $cname[0]['office_contact_email'];
}
$fromEmail = FROM;
$emailData = array(
"customeName" => $customer_name,
"estimator_name"=>$estimator_name,
"booking_title" => !empty($bookingData[0]['booking_title']) ? $bookingData[0]['booking_title']: 'No Title',
"booking_date" => date("d-m-Y",strtotime($bookingData[0]['booking_date'])),
"start_time" => date("h:i a",strtotime($bookingData[0]['start_time'])),
"end_time" => date("h:i a",strtotime($bookingData[0]['end_time'])),
"address" =>isset($bookingData[0]['location']) ? $bookingData[0]['location']: '',
"notes" => isset($bookingData[0]['notes']) && !empty($bookingData[0]['notes'])?$bookingData['notes']:'No Notes For this booking',
"status" => isset($status) && !empty($status)? $status:'',
"bookingId" => $bookingData[0]['id'],
'mainHeading'=> isset($mailHeading) && !empty($mailHeading)?$mailHeading:'',
'smallHeading'=>isset($smallHeading) && !empty($smallHeading)?$smallHeading:'',
'smallHeadingcustomer' => isset($smallHeadingcustomer) && !empty($smallHeadingcustomer)?$smallHeadingcustomer: '',
);
if($bookingData[0]['status']!=3 && $bookingData[0]['status']!=0 || $mailSent == true ){
if(!empty($emailData) && $senttocustomer){
$newBooking= $this->load->view('templates/email-template/new_booking', $emailData, TRUE);
$this->email->from($fromEmail);
$this->email->to($customer_mail);
$this->email->subject($subject);
$this->email->message($newBooking);
$this->email->send();
}
if(!empty($estimator_mail) && $senttoestimator){
$newEstimatorBooking = $this->load->view('templates/email-template/new_booking_estimator', $emailData, TRUE);
$this->email->from($fromEmail);
$this->email->to($estimator_mail);
$this->email->subject($subject);
$this->email->message($newEstimatorBooking);
$this->email->send();
}
}
}
}
else{
return false;
}
return true;
}
/ 删除预订功能 /
public function deleteBooking($id){
$where = array('id'=>$id);
$bookingData = $this->Cnc_model->getData('bookings', array('*'), array('id'=>$id));
if(!empty($bookingData)){
if(!empty($bookingData[0]['estimator_id']) && $bookingData[0]['estimator_id'] != 0){
$name = $this->Cnc_model->getData('users',array('username', 'email'), array('user_id'=> $bookingData[0]['estimator_id']));
$estimator_name = $name[0]['username'];
$estimator_mail = $name[0]['email'];
}
if($bookingData[0]['customer_id'] != 0){
$cname = $this->Cnc_model->getData('customers',array('name', 'office_contact_email'), array('id'=> $bookingData[0]['customer_id']));
$customer_name = $cname[0]['name'];
$customer_mail = $cname[0]['office_contact_email'];
}
$mailHeading = 'Booking Deleted';
$smallHeading = 'Your Booking with '.$customer_name.' on '.$bookingData[0]['booking_date'].' has been deleted by admin.';
$subject = 'Booking Deleted';
$smallHeadingcustomer = 'Your Booking with '.$estimator_name.' on '.$bookingData[0]['booking_date'].' has been deleted by admin.';
$status = 5;
/ *这里我正在呼叫发送电子邮件功能* /
$result = $this->emailSent($id, $mailHeading, $smallHeading, $subject, $smallHeadingcustomer,$status);
/ *获得响应后,我向ajax请求发送响应* /
if($result){
$deleteBooking = $this->Cnc_model->rowsDelete('bookings', $where);
if($deleteBooking){
$this->logThis('Deleted', 'Booking', 'Booking Deleted', array('id' => $id));
$this->Cnc_model->addNotification($id,22,$this->auth_user_id,$bookingData[0]['estimator_id'],'Booking Deleted',$bookingData[0]['booking_title'].' deleted on '.date("m/d/Y").' at '.date('H:i a'));
$where = '';
$data['success'] = true;
$data['message'] = 'Booking deleted successfully.';
echo json_encode($data);
die;
}else{
$where = '';
$data['success'] = false;
$data['message'] = 'Error while deleting booking.';
echo json_encode($data);
die;
}
}
}
else{
$data['success'] = false;
$data['message'] = 'Error while deleting booking.';
echo json_encode($data);
die;
}
}
答案 0 :(得分:0)
这取决于您想走的深度。理想的方法是运行RabbitMQ或Gearman服务器并设置一个“发送邮件”作业供它们处理。提交作业非常快,并且可以使脚本完成并将响应发送回用户,但是当然这将涉及设置更多的基础结构,编写/维护更多的代码以处理该作业以及处理重试/错误状态。
问题最少的方法可能是运行将中继邮件的本地邮件服务器。移交应该非常快,可以提供更好的“有效”响应(至少用户会知道邮件已移交给本地邮件服务器)并允许脚本继续进行。如果电子邮件失败,则由本地邮件服务器将其作为对管理员或“发件人”用户的退回处理。
概述here的一种方法是先发送响应,然后再处理电子邮件-这意味着它永远不会向用户报告失败。不理想,但这取决于您的情况-也许管理员可以处理失败的消息并可以重新发送?
答案 1 :(得分:0)
您可以使用ignore_user_abort(true); 设置客户端断开连接是否应导致脚本中止。这将断开您当前的连接,然后将响应发送到前端,但仍执行其余代码。
ignore_user_abort(true);
ob_start();
echo "success";
$serverProtocole = filter_input(INPUT_SERVER, 'SERVER_PROTOCOL', FILTER_SANITIZE_STRING);
header($serverProtocole.' 200 OK');
header('Content-Encoding: none');
header('Content-Length: '.ob_get_length());
header('Connection: close');
ob_end_flush();
ob_flush();
flush();
最初对您的API使用后端验证,然后使用上面的代码发送响应,然后您可以编写代码通过电子邮件向用户发送电子邮件,这样,前端将获得响应,脚本将执行直到遇到返回或者死;这种方法的问题在于,如果在发送电子邮件时出错,则无法向前端发送响应。
否则,您可以在代码中使用邮件队列来稍后执行工作