5

I have done sudo apt-get install curl, sudo apt-get php5-curl, sudo apt-get php-curl but i get undefined function for curl_init()

does anyone know any solutions for this?

Here is my PHP code.

<?php 
    // create curl resource 
    $ch = curl_init(); 

    // set url 
    curl_setopt($ch, CURLOPT_URL, "example.com"); 

    //return the transfer as a string 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

    // $output contains the output string 
    $output = curl_exec($ch); 

    // close curl resource to free up system resources 
    curl_close($ch);      
?>
AceJordan
  • 221
  • 2
  • 3
  • 9
  • try checking `php -i | grep curl` to see if it outputs anything or check if you see curl in `php -m`. – Ekin Jun 09 '16 at 04:10
  • After i did php -i | grep curl, i get this. /etc/php/7.0/cli/conf.d/20-curl.ini, curl – AceJordan Jun 09 '16 at 04:12
  • this error did not exist on php5 – AceJordan Jun 09 '16 at 04:13
  • okay, how about php -m ? can you see curl in the output? nevertheless you should be able to get going all okay with just `sudo apt-get install php-curl` – Ekin Jun 09 '16 at 04:13
  • It's also very likely the php7 package is called `php7.0-curl` – Ekin Jun 09 '16 at 04:18
  • This is the result for php -m: [PHP Modules] calendar Core ctype curl date dom exif fileinfo filter ftp gd gettext hash iconv json libxml mbstring mcrypt mysqli mysqlnd openssl pcntl pcre PDO pdo_mysql Phar posix readline Reflection session shmop SimpleXML sockets SPL standard sysvmsg sysvsem sysvshm tokenizer wddx xdebug xml xmlreader xmlwriter xsl Zend OPcache zlib – AceJordan Jun 09 '16 at 04:19
  • i have already the latest php7.0-curl. I'm still trying to research more on this because i believe i've installed already all necessary libs for cURL to work – AceJordan Jun 09 '16 at 04:20
  • Did you restart apache (if you are using apache2) `sudo /etc/init.d/apache2 restart` or `service apache2 restart` afterwards? It does seem like you have it installed just not enabled. :) – Ekin Jun 09 '16 at 04:22

4 Answers4

5

Check what version of PHP is apache using ,By using phpinfo(); function.

Checking is important because you might have switched to lower version for any reason (Like it was in my case).

Now from command line run this command to install curl

*(Don\'t copy this) sudo apt-get install php[version of php]-curl* 

For example if it is 5.6 then use

sudo apt-get install php5.6-curl 

After that, don't forget to restart apache2

sudo service apache2 restart
Confused
  • 1,602
  • 15
  • 25
  • I was facing same issue with installed php7 curl.. But when i checked I found that the enable php version was 5.6 So it is for some1 who might face same – Confused Sep 01 '16 at 19:43
4

For Ubuntu 16.04 running php7.0, you should specify the version like so:

sudo apt-get install php7.0-curl

Then, as always, restart apache with

sudo service apache2 reload

3

Try this: phpenmod curl and restart apache. Hope this help (;

trank
  • 952
  • 11
  • 8
  • i tried this and I got this result 'Module curl ini file doesn't exist under /etc/php/5.6/mods-available.' , i guess it's getting curl in the wrong directory since i'm using PHP 7 – AceJordan Jun 09 '16 at 06:17
  • 1
    try enabling php7 mod like @Ekn's comment and specify php version in `phpenmod` command like: `phpenmod -v 7.0 curl` – trank Jun 09 '16 at 09:03
2

For anyone having a similar issue when installing a php extension and still getting undefined:

  1. Run php -i | grep EXT_NAME to see the output if the regex matches anything that has EXT_NAME (in above case curl) in your phpinfo() or simply make a test file with <?php phpinfo(); ?> and see if you spot the extension.
  2. Run php -m to see loaded extensions and check if you have another installation that is taking precedence via php -v or more than one php.ini file using find -name 'php.ini' or locate php.ini in the root directory.
  3. Don't forget to restart apache (or any other similar service) and/or enable if you have installed a module.

3# for apache can be done via running service apache2 restart

If you're building from source another possible cause might be that you forgot to recompile PHP with (again, in this case curl) --with-curl[=DIR] after installing an extension.

Ekin
  • 1,957
  • 2
  • 31
  • 45
  • 1
    @AceJordan you might need to disable mod php5 and enable php7 via: `a2dismod php5` and then `a2enmod php7.0` then `service apache2 restart` – Ekin Jun 09 '16 at 07:39
  • That of course requires you to install `libapache2-mod-php7.0` first. – Ekin Jun 09 '16 at 07:41