我想在不刷新页面的情况下自动调用 laravel PHP 中的函数
i want to automatically call a function in laravel PHP without refreshing page
我在 la-ravel 控制器中有一个功能,我用它来使用 IMAP 客户端从我的 gmail 获取电子邮件,但我想总是收到新消息而不必刷新网页,但我不知道如何获取它完成,请求帮助,下面是我的功能代码;
public function mail()
{
$last = DB::table('mails')->latest()->first();
$oClient = Client::account('default');
$oClient->connect();
$oFolder = $oClient->getFolders('Gmail.unread');
/*$date=$last->date;
$date=Carbon::createFromFormat('Y-m-d', $date)->format('d.m.Y');*/
foreach($oFolder as $oFolder){
// dd($oFolder);
try {
$message = $oFolder->query()->from('xxxxxxgmail.com')->since('25.04.2020')->get();
$msg=$message->get('items');
if ($msg='null') {
$mails=mail::where('status',1)
->get();
return view('mail', compact('mails'));
}
}
catch (ModelNotFoundException $exception)
{
$mails=mail::where('status',1)
->get();
return view('mail', compact('mails'));
}
foreach($message as $message){
$message= $message->getTextBody();
$calling= preg_match("/Calling number(.*), /", $message,$matches);
$calling=preg_match("/ (.*), /", $matches[1],$match);
$calling=$match[1];
$messages=str_replace("\r\n\r\n", '', $message);
$callback= preg_match("/Callback phone(.*)C/", $messages,$cb);
$callback = ltrim($cb[1], '?');
$name= preg_match("/name(.*)C/", $messages,$cb);
$result=$cb[1];
$name= preg_match("/(.*)C/", $result,$cb);
$name=ltrim($cb[1], '?');
$callcount= preg_match("/Calling number(.*), /", $messages,$matches);
$callcount=preg_match("/,(.*),/", $matches[0],$match);
$callcount=preg_match("/:(.*)/", $match[1],$match);
$callcount=$match[1];
$date= preg_match("/Date(.*) /", $messages,$cb);
$date=preg_match("/:(.*),/", $cb[1],$match);
$date=$match[1];
$callscript= preg_match("/Call script(.*) /", $messages,$cb);
$callscript =$cb[1];
$date=strtotime($date);
$date = date('Y-m-d',$date);
$mail=new mail();
$mail->date=$date;
$mail->name=$name;
$mail->calling_no=$calling;
$mail->callback_phone=$callback;
$mail->message=$callscript;
$mail->save();
$mails=mail::where('status',1)
->get();
return view('mail', compact('mails'));
}
}
}
我想要的是一个 java script/AJAX 代码来自动调用这个函数而不刷新网页
您必须像这样使用 get
方法并获取带有时间段的新消息,然后像这样使用
function getNewMessage() {
$.ajax({
type: "GET",
url: "{{url('')}}/mail",
success: function (response) {
console.log(response);
}
});
}
setTimeout(getNewMessage, 1000); // 1000 indicated 1 second
我在 la-ravel 控制器中有一个功能,我用它来使用 IMAP 客户端从我的 gmail 获取电子邮件,但我想总是收到新消息而不必刷新网页,但我不知道如何获取它完成,请求帮助,下面是我的功能代码;
public function mail()
{
$last = DB::table('mails')->latest()->first();
$oClient = Client::account('default');
$oClient->connect();
$oFolder = $oClient->getFolders('Gmail.unread');
/*$date=$last->date;
$date=Carbon::createFromFormat('Y-m-d', $date)->format('d.m.Y');*/
foreach($oFolder as $oFolder){
// dd($oFolder);
try {
$message = $oFolder->query()->from('xxxxxxgmail.com')->since('25.04.2020')->get();
$msg=$message->get('items');
if ($msg='null') {
$mails=mail::where('status',1)
->get();
return view('mail', compact('mails'));
}
}
catch (ModelNotFoundException $exception)
{
$mails=mail::where('status',1)
->get();
return view('mail', compact('mails'));
}
foreach($message as $message){
$message= $message->getTextBody();
$calling= preg_match("/Calling number(.*), /", $message,$matches);
$calling=preg_match("/ (.*), /", $matches[1],$match);
$calling=$match[1];
$messages=str_replace("\r\n\r\n", '', $message);
$callback= preg_match("/Callback phone(.*)C/", $messages,$cb);
$callback = ltrim($cb[1], '?');
$name= preg_match("/name(.*)C/", $messages,$cb);
$result=$cb[1];
$name= preg_match("/(.*)C/", $result,$cb);
$name=ltrim($cb[1], '?');
$callcount= preg_match("/Calling number(.*), /", $messages,$matches);
$callcount=preg_match("/,(.*),/", $matches[0],$match);
$callcount=preg_match("/:(.*)/", $match[1],$match);
$callcount=$match[1];
$date= preg_match("/Date(.*) /", $messages,$cb);
$date=preg_match("/:(.*),/", $cb[1],$match);
$date=$match[1];
$callscript= preg_match("/Call script(.*) /", $messages,$cb);
$callscript =$cb[1];
$date=strtotime($date);
$date = date('Y-m-d',$date);
$mail=new mail();
$mail->date=$date;
$mail->name=$name;
$mail->calling_no=$calling;
$mail->callback_phone=$callback;
$mail->message=$callscript;
$mail->save();
$mails=mail::where('status',1)
->get();
return view('mail', compact('mails'));
}
}
}
我想要的是一个 java script/AJAX 代码来自动调用这个函数而不刷新网页
您必须像这样使用 get
方法并获取带有时间段的新消息,然后像这样使用
function getNewMessage() {
$.ajax({
type: "GET",
url: "{{url('')}}/mail",
success: function (response) {
console.log(response);
}
});
}
setTimeout(getNewMessage, 1000); // 1000 indicated 1 second