[英]Curl request not working in laravel 7 but working inside the command line
我使用 laravel 7 作為后端制作了一個應用程序,一切都很酷,除非我嘗試使用 firebase 發送通知時,我似乎無法讓 curl 工作。
我試過這段代碼,但 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-curl
擴展。
任何 curl php 請求的響應都是0
或false
。
在不工作的服務器中添加這樣的擴展:
sudo yum install -y php-curl
然后重新啟動您的網絡服務器
sudo systemctl restart httpd.service
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.