我试图使用卷曲在Laravel中使用FCM,但我会遇到错误。首先,我在我的一个共滚子中写了一个PHP代码:
$first_name = $request->input('first_name');
//FCM api URL
$url = 'https://fcm.googleapis.com/fcm/send';
//api_key available in Firebase Console -> Project Settings -> CLOUD MESSAGING -> Server key
$server_key = 'AIzaSyA1RyuAGGPASh_flFCwiyd9ZHEMYlhQOho';
$target = "r_token";
$fields = array();
$fields['data'] = $first_name;
if(is_array($target)){
$fields['registration_ids'] = $target;
}else{
$fields['to'] = $target;
}
//header with content_type api key
$headers = array(
'Content-Type:application/json',
'Authorization:key='.$server_key
);
$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_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);
return $result;
我试图使用Laravel 5.2在控制器中运行此代码,但我遇到了此错误:
FatalErrorException in WebKyoController.php line 52:
Call to undefined function AppHttpControllerscurl_init()
我尝试过:sudo apt-get安装php-curl和apache重新启动,但我仍然会遇到错误。我只想知道我需要做什么。
我偶然发现了这个答案,所以以防万一不幸的灵魂跟随,请尝试以下对我有用的以下内容。
- 找出运行php的版本
# php -v
PHP 7.0.28-0ubuntu0.16.04.1 (cli) ( NTS ) ...
在这种情况下,我有php 7.0
- 您的PHP版本安装卷发
sudo apt install php7.0-curl
- 要立即应用此安装运行
sudo service apache2 restart
- 再次运行代码,看看它是否不再在卷发上出错。
不知道版本php
sudo服务apache2 restart
sudo apt-get安装php-curl
我找到了我已经安装了php 7的解决方案,但它无法正常工作。但是我删除了PHP 7并安装了PHP 5,然后运行以下命令:
sudo apt-get install curl
sudo service apache2 restart
sudo apt-get install php5-curl
sudo service apache2 restart
上面的命令为我带来了技巧。我不知道为什么它在PHP 7上不起作用