繁体   English   中英

Laravel 5.2 curl_init() 抛出错误“调用未定义的函数”

[英]Laravel 5.2 curl_init() throwing error “Call to undefined function”

我正在尝试使用 curl 在 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 App\Http\Controllers\curl_init()

我试过: sudo apt-get install php-curl 和 apache restart 但我仍然收到错误。 我只想知道我需要做什么。

我偶然发现了这个答案,所以以防万一另一个不幸的灵魂跟随,请尝试以下对我有用的方法。

  1. 找出运行的PHP版本

# php -v PHP 7.0.28-0ubuntu0.16.04.1 (cli) ( NTS ) ...

在这种情况下,我有 PHP 7.0

  1. 为您的 PHP 版本安装 Curl

sudo apt install php7.0-curl

  1. 立即应用此安装运行

sudo service apache2 restart

  1. 再次运行您的代码,看看它是否不再在 Curl 上出错。

不知道 php 版本

须藤服务 apache2 重启

须藤 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 上不起作用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM