Are you using PHP’s curl_exec() function and getting a “call to undefined function curl_init()” error? If so, then let’s fix the undefined curl_init() function error for you on Linux.
Table of Contents
What is cURL?
cURL is a library (known as libcurl) that lets you communicate with other computers in PHP. In other words, PHP uses the libcurl extension to transfer data to or from a remote computer. libcurl currently supports HTTP/HTTPS, FTP, and many other protocols. You can also POST, GET and do other methods.
Cause of the curl_init() undefined function error
You will get a fatal PHP error about an undefined function called: “curl_init()” if the extension is not installed or enabled. Therefore, you need to install the libcurl package to use cURL functions.
To use the CURL command, you would need to use code similar to this:
$ch = curl_init();
curl_setopt_array($ch, $this->getCurlGetOptions($this->getUrl($params)));
$response = curl_exec($ch);
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$headerText = substr($response, 0, $headerSize);
$bodyText = substr($response, $headerSize);
$headers = $this->getHeadersFromCurl($headerText);
If the PHP CURL module is not found, you will see an error similar to this:
PHP Fatal error: Uncaught Error: Call to undefined function curl_init()
Let’s fix the undefined function curl_init() error
Again, the fix will depend on your web hosting setup:
If you have no access to the command line:
You must contact your web hosting provider to enable cURL if you do not have command-line access. The same is true if you do not have access to php.ini. Some web hosting providers disable this extension by default for security reasons, but they may enable it for you on request. Additionally, you may be able to enable cURL if you have access to CPanel.
If you have access to the command line:
You will first need to install libcurl and then restart your web server.
Install libcurl
First, install libcurl for PHP on Debian/Ubuntu:
sudo apt-get install php-curl
If your distro is RHEL/Centos, then run this command:
sudo yum install php-curl
If you use Fedora, then run this command:
sudo dnf install php-curl
Restart your web server
Then, restart your webserver if it is Apache on Debian/Ubuntu:
sudo service apache2 restart
Or restart your webserver if it is Nginx on Debian/Ubuntu/Fedora:
sudo service nginx restart
Alternatively, restart your webserver if it is Apache on RHEL/CentOS/Fedora:
sudo systemctl restart httpd
Or restart your webserver if it is Nginx on RHEL/CentOS/Fedora:
sudo systemctl restart nginx
Wrapping up undefined function curl_init error
You will no longer get a “call to undefined function curl_init()” error when using PHP’s cURL functions.
You may also be interested in
Source: