PHP Fatal error: Call to undefined function curl_init()
Asked Answered
M

24

78

I try PHP Post Request inside a POST Request thinking it might be useful for me. My code is given below:

$sub_req_url = "http://localhost/index1.php";

$ch = curl_init($sub_req_url);
$encoded = '';

// include GET as well as POST variables; your needs may vary.
foreach($_GET as $name => $value) {
  $encoded .= urlencode($name).'='.urlencode($value).'&';
}

foreach($_POST as $name => $value) {
  $encoded .= urlencode($name).'='.urlencode($value).'&';
}

// chop off last ampersand
$encoded = substr($encoded, 0, strlen($encoded)-1);

curl_setopt($ch, CURLOPT_POSTFIELDS,  $encoded);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_exec($ch);
curl_close($ch);

from the index.php file and index2.php is another file in the same directory and when I open the page I get the following error in my error.log file:

[Sat Dec 18 15:24:53 2010] [error] [client ::1] PHP Fatal error: Call to undefined function curl_init() in /var/www/testing1/index.php on line 5

What I want to do is to have a reservation form that send post request. Then I want to process post values and send again the post request to paypal.

Maldonado answered 18/12, 2010 at 9:45 Comment(3)
seems like curl library is not enabledNonessential
yeah i cannot find curl in my phpinfoMaldonado
also i cannot find curl in my php.ini fileMaldonado
S
148

You need to install CURL support for php.

In Ubuntu you can install it via

sudo apt-get install php5-curl

If you're using apt-get then you won't need to edit any PHP configuration, but you will need to restart your Apache.

sudo /etc/init.d/apache2 restart

If you're still getting issues, then try and use phpinfo() to make sure that CURL is listed as installed. (If it isn't, then you may need to open another question, asking why your packages aren't installing.)

There is an installation manual in the PHP CURL documentation.

Siesta answered 18/12, 2010 at 9:48 Comment(7)
user@explorerx:~$ sudo apt-get install php5_curl Reading package lists... Done Building dependency tree Reading state information... Done E: Couldn't find package php5_curlMaldonado
I thing you are right but it's php5-curl. I am doing it let's seeMaldonado
php-curl, not php_curl - dash, no underscore.Helsinki
I also needed to do sudo php5enmod curl between installing and restarting apache. Probably due to recent changes in the Debian packaging.Carbazole
in ubuntu 16.04 $ sudo apt-get install -y php5.6-curlSpikes
for a fresh ubuntu 18 sudo apt install php-curl worked for meMicrophotograph
This was probably good in the past, but the issue with this error was solved by what I outlined in my answer below. 2019, yay!Westlund
O
26

For Windows, if anybody is interested, uncomment the following line (by removing the ;) from php.ini

;extension=php_curl.dll

Restart apache server.

Open answered 30/6, 2013 at 15:39 Comment(3)
For XAMPP users, this php.ini is located in your /xampp/apache/php/. Older versions have it at /xampp/apache/bin/.Bigelow
For Ubuntu users, the php.ini file locations are listed here. And note that there are 2 php.ini files for Apache and CLI (command line)Metalinguistic
On current version of PHP for Windows, it's now actually "extension=curl" instead of "extension=php_curl.dll"Portauprince
M
10

I got it working in ubuntu 16.04 by following steps.My php version was 7.0

sudo apt-get install php7.0-curl

sudo service apache2 restart

i got it from this link check here for more details

Muzzleloader answered 7/8, 2017 at 5:29 Comment(0)
B
6

For Ubuntu: add extension=php_curl.so to php.ini to enable, if necessary. Then sudo service apache2 restart

this is generally taken care of automatically, but there are situations - eg, in shared development environments - where it can become necessary to re-enable manually.

The thumbprint will match all three of these conditions:

  1. Fatal Error on curl_init() call
  2. in php_info, you will see the curl module author (indicating curl is installed and available)
  3. also in php_info, you will see no curl config block (indicating curl wasn't loaded)
Brahmanism answered 15/3, 2015 at 13:28 Comment(0)
W
4

Had this problem but found the answer here: https://askubuntu.com/questions/1116448/cannot-enable-php-curl-on-ubuntu-18-04-php-7-2

Had to:

sudo a2dismod php7.0

And:

sudo a2enmod php7.2

Then:

sudo service apache2 restart

This was after a system upgrade to next version of Ubuntu. All the other answers I found were stale, due to a bad cert apparently on the PPA most of them pointed out, but would probably not have worked anyway. The real issue was disabling the old versions of php, apparently.

Found the solution here:

https://askubuntu.com/questions/1116448/cannot-enable-php-curl-on-ubuntu-18-04-php-7-2

Westlund answered 26/6, 2019 at 10:11 Comment(2)
First I had to install libapache2-mod-php7.3 then follow these useful steps with the proper versions of course.Troat
I had to disable 8.0 when running 8.1 actuallyMonovalent
R
3

In my case, in Xubuntu, I had to install libcurl3 libcurl3-dev libraries. With this command everything worked:

sudo apt-get install curl libcurl3 libcurl3-dev php5-curl
Rebecarebecca answered 30/1, 2015 at 17:5 Comment(0)
P
3

Just adding my answer for the case where there are multiple versions of PHP installed in your system, and you are sure that you have already installed the php-curl package, and yet Apache is still giving you the same error.

curl_init() undefined even if php-curl is enabled in Php 7.

Phenomenology answered 18/2, 2017 at 5:33 Comment(0)
A
2

To fix this bug, I did:

  1. In php.ini file, uncomment this line: extension=php_curl.dll
  2. In php.ini file, uncomment this line: extension_dir = "ext"
  3. I restarted NETBEANS, as I was using Built-in server
Atwitter answered 31/10, 2014 at 17:17 Comment(1)
Point 2. is what fixed it for me. Thanks!!Manufacturer
S
2

I got this error using PHP7 / Apache 2.4 on a windows platform. curl_init worked from CLI but not with Apache 2.4. I resolved it by adding LoadFile directives for libeay32.dll and ssleay32.dll:

LoadFile "C:/path/to/Php7/libeay32.dll"
LoadFile "C:/path/to/Php7/ssleay32.dll"
LoadFile "C:/path/to/Php7/php7ts.dll"
LoadModule php7_module "C:/path/to/Php7/php7apache2_4.dll"
Saylor answered 8/2, 2016 at 11:12 Comment(0)
S
2

This answer is for https request:

Curl doesn't have built-in root certificates (like most modern browser do). You need to explicitly point it to a cacert.pem file:

  curl_setopt($ch, CURLOPT_CAINFO, '/path/to/cert/file/cacert.pem');

Without this, curl cannot verify the certificate sent back via ssl. This same root certificate file can be used every time you use SSL in curl.

You can get the cacert.pem file here: http://curl.haxx.se/docs/caextract.html

Reference PHP cURL Not Working with HTTPS

Shayne answered 17/2, 2017 at 8:58 Comment(0)
B
2

Step 1 :

C:/(path to php folder)/php.ini
enable extension=php_curl.dll

(remove the ; at the end of the line)

Step 2 :

Add this to Apache/conf/httpd.conf (libeay32.dll, ssleay32.dll, libssh2.dll find directly in php7 folder)

# load curl and open ssl libraries
 LoadFile "C:/(path to php folder)/libeay32.dll"
 LoadFile "C:/(path to php folder)/ssleay32.dll"
 LoadFile "C:/(path to php folder)/libssh2.dll"
Brewis answered 10/4, 2018 at 7:24 Comment(1)
To me it works just adding the ".../libssh2.dll" line. PHP version 7.2.4 on Windows 10. Thanks.Gabelle
L
2

On newer versions of PHP on Windows, like PHP 7.x, the corresponding configuration lines suggested on previous answers here, have changed. You need to uncomment (remove the ; at the beginning of the line) the following line:

extension_dir = "ext"
extension=curl
Lenna answered 18/8, 2020 at 2:2 Comment(1)
Recent versions of WIndows PHP don't have a php,ini file by default. See here https://mcmap.net/q/82421/-php-ini-file-nowhere-to-be-foundPostpaid
D
1

(Trying to get Curl working via PHP and Apache on Windows...)

I kept getting an error saying: Call to undefined function 'curl_init()'

I made sure I had enabled curl with this line in my php.ini file: extension=php_curl.dll

I made sure the extension_dir variable was being set properly, like this: extension_dir = "ext"

I was doing everything everyone else said on the forums and curl was not showing up in my call to phpinfo(), and I kept getting that same error from above.

Finally I found out that Apache by default looks for php.ini in the C:\Windows folder. I had been changing php.ini in my PHP installation folder. Once I copied my php.ini into C:\Windows, everything worked.

Took me forever to figure that out, so thought I'd post in case it helps someone else.

Drawstring answered 4/7, 2014 at 3:50 Comment(0)
Q
1

For PHP 7 and Windows x64

libeay32.dll, libssh2.dll and ssleay32.dll should not be in apache/bin and should only exist in php directory and add php directory in system environment variable. This work for me.

Obvisouly in php.ini you should have enable php_curl.dll as well.

Quartzite answered 17/3, 2018 at 19:14 Comment(1)
"add php directory in system environment variable" - which environment variable ?Duodecillion
I
1

Yet another answer ... If you land here in Oct 2020 because PHP on the command line (CLI) has stopped working, guess what ... some upgrades will move you to a different/newer version of PHP silently, without asking!

Run: php --version and you might be surprised to see what version the CLI is running.

Then run: ll /usr/bin/php and you might be surprised to see where this is linking to.

It's best to reference the SPECIFIC version of PHP you want when calling the PHP binary directly and not a symbolic link.

Example:

/usr/bin/php7.3 will give you the exact version you want. You can't trust /usr/bin/php or even just typing php because an upgrade might switch versions on you silently.

Iyeyasu answered 11/10, 2020 at 16:7 Comment(0)
M
0

RusAlex answer is right in that for Apache you have to install and enable curl and restart your apache service:

sudo apt-get install php5-curl
sudo service apache2 restart

On my Ubuntu Server with nginx and php5-fpm I however ran into the following problem. I had to restart nginx and php5-fpm like so:

sudo service nginx restart
sudo service php5-fpm restart

But I had non-working php5-fpm processes hanging around, which apparently is a bug in ubuntu https://bugs.launchpad.net/ubuntu/+source/php5/+bug/1242376 So I had to kill all idle php5-fpm processes to able to restart php5-fpm so that the curl module is actually loaded

sudo kill <Process Id of php5-fpm Process)
Moshemoshell answered 20/7, 2014 at 2:15 Comment(0)
A
0

I also faced this issue. My Operating system is Ubuntu 18.04 and my PHP version is PHP 7.2.

Here's how I solved it:

Install CURL on Ubuntu Server:

sudo apt-get install curl

Add the PHP CURL repository below to your sources list:

sudo add-apt-repository ppa:ondrej/php

Refresh your package database

sudo apt update

Install PHP-curl 7.2

sudo apt install php7.2-fpm php7.2-gd php7.2-curl php7.2-mysql php7.2-dev php7.2-cli php7.2-common php7.2-mbstring php7.2-intl php7.2-zip php7.2-bcmath

Restart Apache Server

sudo systemctl restart apache2

That's all.

I hope this helps

Angeliqueangelis answered 10/1, 2020 at 16:19 Comment(0)
W
0

This worked for me with raspian:

sudo apt update && sudo apt upgrade
sudo apt install php-curl

finally:

sudo systemctl restart apache2

or:

sudo systemctl restart nginx
Walkup answered 18/2, 2021 at 3:5 Comment(0)
D
0

Many answers kind of give the answer without pointing your nose at the actual problem (which may be that you're installing the wrong version of PHP!), so I'll repeat it here, hopefully doing a better job:

If you're on a Linux system (for Windows see other answers), and the suggestion to install "php-curl" didn't work, even after restarting the web service, then it might be that your web browser is not using the latest PHP version - and in that case you need to install an older version of the php-curl libs.

To check which PHP version you're running, either visit a page on the website that contains <?php phpinfo(); ?> or check your server's settings to see which PHP version it loads.

For instance, if you find out that the web server uses PHP 7.4, then install the matching libs with: sudo apt-get install php7.4-curl. Then restart the web server and see if that works.

Dowden answered 10/2 at 13:45 Comment(0)
I
-1

function curl_int(); cause server error,install sudo apt-get install php5-curl restart apache2 server .. it will work like charm

Inculpable answered 12/3, 2016 at 8:21 Comment(0)
M
-1

For linux you can install it via

sudo apt-get install php5-curl

For Windows(removing the ;) from php.ini

;extension=php_curl.dll

Restart apache server.

Mitinger answered 13/5, 2019 at 8:31 Comment(0)
I
-1

On Ubuntu 18.04 these two commands solve my this problem.

sudo apt-get install php5.6-curl //install curl for php 5.6
sudo service apache2 restart //restart apache
Isodiametric answered 21/6, 2019 at 10:35 Comment(0)
S
-1

Seems you haven't installed the Curl on your server.
Check the PHP version of your server and run the following command to install the curl.

sudo apt-get install php7.2-curl

Then restart the apache service by using the following command.

sudo service apache2 restart

Replace 7.2 with your PHP version.

Sandbag answered 16/9, 2019 at 8:39 Comment(0)
P
-1

To install the last version of php-curl on Ubuntu, use this:

sudo apt-get install php-curl -y

Pyrite answered 9/11, 2021 at 14:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.