Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call to undefined function curl_init() even it is enabled in php7

Tags:

php

curl

ubuntu

I've just installed php7 to my Ubuntu. At first, there was no problem, my web site was working. But suddenly, it started to return Call to undefined function curl_init() error. Now, my pages contain curl codes do not work.

In phpinfo(), it looks Curl is enabled. There were similar questions but none of them handled it in php7. I thought it should be something different than others.

Edit: When I try

php -i | grep curl        

in terminal, it returns

/etc/php/7.0/cli/conf.d/20-curl.ini, curl 
like image 702
Sabri Karagönen Avatar asked Jan 17 '16 19:01

Sabri Karagönen


People also ask

What is Curl_init?

The curl_init() function will initialize a new session and return a cURL handle. curl_exec($ch) function should be called after initialize a cURL session and all the options for the session are set. Its purpose is simply to execute the predefined CURL session (given by ch).

How do I know if cURL is enabled in PHP?

Create phpinfo. php file and save. phpinfo; ?> Then go to http://domainname/phpinfo.php to check whether CURL is enabled or not.


2 Answers

I've had similar problem with curl after upgrade to XX (16.04). After reinstalling curl with:

sudo apt-get install php-curl 

And server restart

sudo service apache2 restart 

everything went back to normal :)

like image 73
Deus777 Avatar answered Sep 19 '22 17:09

Deus777


Assumption

You've installed the version of the module for the PHP version you are using, and yet the problem is not going away.

What is going on here?

There could be multiple versions of PHP installed on your system and Apache is not using the version you are expecting it to use.

How do you know which version of PHP Apache is using?

To know this, the key idea is to learn the ROOT directory of your Apache's configuration files. In the command line, you can type:

apache2ctl -V  //sample output below  AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message Server version: Apache/2.4.7 (Ubuntu) Server built:   Jul 15 2016 15:34:04 Server's Module Magic Number: 20120211:27 Server loaded:  APR 1.5.1-dev, APR-UTIL 1.5.3 Compiled using: APR 1.5.1-dev, APR-UTIL 1.5.3 Architecture:   64-bit Server MPM:     prefork   threaded:     no     forked:     yes (variable process count) Server compiled with....  -D APR_HAS_SENDFILE  -D APR_HAS_MMAP  -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)  -D APR_USE_SYSVSEM_SERIALIZE  -D APR_USE_PTHREAD_SERIALIZE  -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT  -D APR_HAS_OTHER_CHILD  -D AP_HAVE_RELIABLE_PIPED_LOGS  -D DYNAMIC_MODULE_LIMIT=256  -D HTTPD_ROOT="/etc/apache2"  -D SUEXEC_BIN="/usr/lib/apache2/suexec"  -D DEFAULT_PIDLOG="/var/run/apache2.pid"  -D DEFAULT_SCOREBOARD="logs/apache_runtime_status"  -D DEFAULT_ERRORLOG="logs/error_log"  -D AP_TYPES_CONFIG_FILE="mime.types"  -D SERVER_CONFIG_FILE="apache2.conf" 

In my case, my Apache's ROOT configuration directory is shown in the

HTTPD_ROOT="/etc/apache2" 

Now that I know the location of the configs that Apache is using, I can now accurately determine the version of PHP it is using by examining the "mods-enabled" directory located inside the "/etc/apache2" directory.

In my case, when do an ls while inside the "mods-enabled", it showed the ff output:

access_compat.load  authz_user.load  filter.load       php5.load ... authz_host.load     env.load         php5.conf 

At this point, I now know for certain that Apache is using the 'php5' version of PHP installed on my system, whatever that may be.

Then I tried to reproduce the error above using this version of PHP (i.e., 'php5') by running the command below:

$ php5 -r "curl_init();" PHP Fatal error:  Call to undefined function curl_init() in Command line code on line 1 

Voila!

The version of PHP that I expected my Apache was using is "php5.6" and running the same command above with this version did not produce the said error.

Solution

To solve this problem, you either install the version of the module that corresponds to the PHP version that Apache is using (in my example php5.0-curl) or you may change the version of PHP that is being used in Apache to the version you want.

How do I tell Apache which version of PHP to use?

You can accomplish this using the a2enmod/a2dismod cli commands of Apache2.

Firstly, I disable the PHP module that is currently active on my server (i.e., "php5"):

a2dismod php5 

Then I enabled the php module for the version of PHP that I want my Apache to use:

a2enmod php5.6 

Then I restart Apache

service apache2 restart 

After I refreshed the offending page on my website, the error is now gone.

like image 42
ultrajohn Avatar answered Sep 21 '22 17:09

ultrajohn