Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.2 curl_init() throwing error "Call to undefined function"

I am trying to use FCM in laravel using curl but I am getting error. First I have written one php code in one of my cotroller which is :

$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;

And I am trying to run this code in my controller using laravel 5.2 but I am getting this error:

  FatalErrorException in WebKyoController.php line 52:
   Call to undefined function App\Http\Controllers\curl_init()

I have tried : sudo apt-get install php-curl and apache restart but still I am getting error. I just want to know what do I need to do.

like image 204
Nilay Singh Avatar asked Dec 20 '16 04:12

Nilay Singh


1 Answers

I stumbled across this answer, so just in case another hapless soul follows, please try the following that worked for me.

  1. Find out the version of PHP running

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

In this case, I have PHP 7.0

  1. Install Curl for your version of PHP

sudo apt install php7.0-curl

  1. To instantly apply this installation run

sudo service apache2 restart

  1. Run your code again and see if it no longer errors on Curl.
like image 81
Ari Avatar answered Oct 29 '22 12:10

Ari