我们如何发送生日和周年纪念短信的设置,例如我们可以使用Laravel框架在用户移动设备中创建多个要触发的短信和生日或周年纪念日前的天数?
答案 0 :(得分:0)
在Laravel中使用Cronjobs你可以通过制作一个像SMSController一样轻松实现这一点(这里我们使用两个表客户端和sms_settings) 守则是:
class SMSController extends Controller{
public function test(Request $request){
$date =Carbon::now(); //current system date now
$date = $date->format('m/d');
// For Birthday SMS
$schedule = DB::table('sms_settings')->select('scheduled_type','number_of_days')->where('occasion','Birthday')->get();
foreach($schedule as $key=>$results){
$number_of_days = $results->number_of_days;
$scheduled_type = $results->scheduled_type;
// select clients with dob
if($scheduled_type=='Before'){
$required_date = Carbon::now()->addDays($number_of_days);
}
if($scheduled_type=='After'){
$required_date = Carbon::now()->subDays($number_of_days);
}
$required_month = $required_date->format('m');
$required_day = $required_date->format('d');
$req = $required_month."-".$required_day;
$results = DB::table('clients')->select('dob','mobile','first_name','last_name')->where('dob','LIKE','%'.$req.'%')->get();
$r1 = DB::table('clients')->select('dob','mobile','first_name','last_name')->where('dob','LIKE','%'.$req.'%')->first();
if(!$r1=null)
{
foreach($results as $key=>$result)
{
$dob = $result->dob;
$mobile_list = $result->mobile;
$first_name = $result->first_name;
$last_name = $result->last_name;
// dd($mobile_list);
$a = DB::table('sms_settings')->select('message_header','message')->where('occasion','Birthday')->get();
foreach($a as $key=>$results){
$header = $results->message_header;
$mbody = $results->message;
}
$message_text = $header." ".$first_name.","."\n". $mbody;
$authKey = urlencode('Your Auth Key');
// Message details
$mobiles = array($mobile_list);
$sender = urlencode('123456');
$message = rawurlencode($message_text);
$route=2;
$country=5;
$mobiles = implode(',', $mobiles);
// Prepare data for POST request
$data = array('authkey' => $authKey, 'mobiles' => $mobiles, "message" => $message, "sender" => $sender,"route" =>$route,"country" =>$country );
// Send the POST request with cURL
$ch = curl_init('your url');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
// Process your response here
}
// Execute it via performing cronjobs
}
}
//对于周年纪念短信请按照上述步骤进行操作
最后你必须输入你的服务器的Cron条目,并通过在Laravel中的Kernel.php文件中定位schedule方法来调用它:
protected function schedule(Schedule $schedule){
$schedule->call('App\Http\Controllers\SMSController@test')->everyMinute();
// Perform your task every minutes.
}