This error will occur if your server does not have PHP’s curl extension installed or enabled.
The error will read something like this.
Uncaught error: Call to undefined function curl_init()
Essentially, PHP can’t find the curl_init function because the extension that defines it has not been loaded. This results in a fatal error, which kills the PHP script.
To avoid this kind of error, you can check to see whether the cURL module has been loaded or not before you attempt to use it.
Check to see if curl is enabled.
To see if your PHP installation has cURL enabled, you can run the following piece of code.
<?php //Call the phpinfo function. phpinfo();
The phpinfo function above will output information about PHP’s configuration.
If a CTRL + F search for “curl” is unable to find anything, then it means that your web server has not loaded the curl extension.
If you do find it, then you should see the following line under the cURL heading:
cURL support: enabled
Note that you should NOT leave this phpinfo function on a live web server, as it outputs sensitive information about your PHP installation!
Enabling curl on Linux.
If your web server is running on Linux and you have SSH / terminal access, then you can install and enable cURL by running the following command.
sudo apt-get install php-curl
After running the command above, you will need to restart your web server so that the changes will take effect.
If you are using Apache, you can restart your web server like so.
sudo /etc/init.d/apache2 restart
If you are using Nginx, you can use the following command.
sudo /etc/init.d/nginx restart
After your web server has been restarted, curl should be available.
Enabling curl on Windows.
If you are using Windows, you will need to locate the php.ini file that is being used by your web server.
If you are unsure about which php.ini file you need to edit, then you can use the phpinfo script that we used above, as that will display the full path to the file that is being used by the web server.
Once you have located your php.ini file, you will need to “uncomment” the following line.
To uncomment the line above and enable the php_curl.dll extension, simply remove the semi-colon at the beginning.
After you have saved the changes that you made to your php.ini file, you will need to restart your web server. Otherwise, the new configuration will not take effect.
Enabling curl on WampServer.
If you are using the popular WampServer program on Windows, then you can try the following steps:
- Click on the WampServer icon in your system tray.
- Hover over the “PHP” option.
- Once the PHP menu appears, hover over the “PHP extensions” option.
- At this stage, a list of PHP extensions should appear. If an extension has a tick beside it, then it is already enabled. If the php_curl option does not have a tick beside it, then it is not enabled. To enable curl, simply click on the php_curl option.
- WampServer should automatically restart Apache and the changes should take effect.
- If WampServer does not automatically restart Apache, then you can manually force it to do so by clicking on the “Restart All Services” option in the main menu.
Hopefully, this guide helped you to get rid of that nasty “undefined function curl_init” error!
Related: Sending a POST request without cURL.