How to resolve call to undefined curl_init()?

  curl, laravel, php

There is a fatal error when I use curl from Laravel but a curl that exists in another file is working.

Why do these errors occur?

fatal error: call to undefined function AppHttpControllerscurl_init()

AppHttpControllerFcmController:

class FcmController extends Controller
{
    public function index()
    {
        $ch = curl_init();

        curl_setopt($ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
        curl_setopt($ch,CURLOPT_POST, true);
        curl_setopt($ch,CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch,CURLOPT_POSTFIELDS, json_encode($fields));

        $result = curl_exec($ch);
        dd($result);

        curl_close($ch);

AppHttpControllerCurlContoller (It works):

class CurlController extends Controller
{
    public static function getCurl($url, $params)
    {
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url . $params);        
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, TRUE);;
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type:application/json; charset=utf-8"));

        $response = curl_exec($ch);
        curl_close($ch); 

        return json_encode($response);
    }

enter image description here

Source: Laravel

Leave a Reply