0

I get following error when I try to use curl_init('some_url') in a controller:

Call to undefined function App\Http\Controllers\curl_init()

but when I use php artisan tinker and try to use curl_init('some_url') it works fine and I get no error.

More Info:

when I type following command in terminal

php -i | grep curl

I get:

/etc/php/7.2/cli/conf.d/20-curl.ini,
curl

as answered in this link I tried reinstalling curl with:

sudo apt-get install php-curl

and I restarted apache with

sudo service apache2 restart

but I still get mentioned error when trying to use curl_init() in a controller. How can I fix this error?

eylay
  • 1,712
  • 4
  • 30
  • 54

2 Answers2

3

its a namespace issue, you're trying to call curl_init from the App\Http\Controllers namespace. to call it from the global namespace, do $ch= \curl_init(); , notice the \ there. for namespace documentation, check http://php.net/manual/en/language.namespaces.php

hanshenrik
  • 19,904
  • 4
  • 43
  • 89
  • this time I get Call to undefined function curl_init() instead of App\Http\Controllers\curl_init() – eylay May 06 '18 at 10:40
  • 1
    @AliSeyfi separate issue, you don't have the curl extension installed in PHP. if you're compiling PHP from source, check http://php.net/manual/en/curl.setup.php (aka, compile with the `--with-curl` flag) , otherwise check with your php provider – hanshenrik May 06 '18 at 10:53
0

Do the following as they worked for me.

  1. Install curl using the following command via terminal.

sudo apt-get install php-curl

  1. Restart the server.

For Apache

systemctl restart apache

For NGinX

sudo systemctl reload nginx

sudo service nginx restart

Junior
  • 1,007
  • 4
  • 16
  • 26