【发布时间】:2021-02-24 05:54:56
【问题描述】:
我用 laravel 7 作为后端制作了一个应用程序,一切都很酷,除了当我尝试使用 firebase 发送通知时,我似乎无法让 curl 工作。
我尝试了this 代码,但 curl 似乎不起作用,如果我直接从命令行尝试服务器上 php 上的代码,代码就可以工作。
我的php版本是7.4.12,系统是centos 8
前端:
<form method="POST" action="{{route('bulksend')}}">
{{ csrf_field()}}
<label>Title</label>
<input type="text" hint="Title" name="title">
<br>
<label>Body</label>
<input type="text" hint="Body" name="body">
<br>
<label>Image URL</label>
<input type="text" hint="Image URL" name="img">
<br>
<label>ID</label>
<input type="text" hint="Image URL" name="id">
<br>
<input type="submit"/>
</form>
控制器方法:
public function bulksend(Request $req)
{
$url = 'https://fcm.googleapis.com/fcm/send';
$dataArr = array('click_action' => 'FLUTTER_NOTIFICATION_CLICK', 'id' => $req->id, 'status' => "done");
$notification = array('title' => $req->title, 'text' => $req->body, 'image' => $req->img, 'sound' => 'default', 'badge' => '1',);
$arrayToSend = array('to' => "my device_token", 'notification' => $notification, 'data' => $dataArr, 'priority' => 'high');
$fields = json_encode($arrayToSend);
$headers = array(
'Authorization: key=' . "my auth",
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$result = curl_exec($ch);
curl_close($ch);
print_r($result);
return $result;
}
输出只是一个空页面。 在控制台中它打印成功。 感谢您的帮助。
【问题讨论】:
-
由于某种未知原因,代码在两天后工作
标签: php laravel flutter laravel-7 php-curl