19

I'm trying to set up a cronjob which requires curl, and I'm calling it directly from crontab with

* * * * * /usr/bin/php myurl/my_cron.php

The problem is, it looks like the curl module isn't installed for my phpcli.

It works just fine when I hit the url from my browser, but when I run

php -q myfile.php

from the command line, it returns

PHP Fatal error:  Call to undefined function curl_init() in my_cron.php on line 20

When I run php -m the curl module does not show up. However when I go to the browser and dump the php_info(), the module shows up and says its correctly installed.

The other kicker is i've been trying to install curl with apt-get onto the server (Ubuntu 12.04 php 5.4), it seems to take down my PHP as it begins to simply attempt to download the index.php file wherever I try to browse to.

Here are the attempts I've made to install curl that have taken down PHP:

sudo apt-get install php-curl
sudo apt-get install curl libcurl3 libcurl3-dev php5-curl

After each of these I restarted the apache2 server and still no dice, it attempted to download the file instead of opening the page.

How can I install php5-curl to just the cli, so that my server can run it and I don't have to go through a browser?

The other possibility is I could run the cronjobs through wget from the crontab file, but I've heard that's not the best option and potentially unreliable.

Any help is much appreciated. Thanks!

Paulie-C
  • 1,674
  • 1
  • 13
  • 29
user2966822
  • 191
  • 1
  • 1
  • 3
  • This question may possibly be a duplicate. I've found a couple of similar questions, like this one: http://stackoverflow.com/questions/10226983/call-to-undefined-function-curl-init – JaidynReiman Nov 07 '13 at 23:04

7 Answers7

45

I had the same issue. But, finally I solved it by running the following command.

sudo apt-get install php7.0-curl

Restart the server after installing. This answer may not be useful for the user who asked because he asked it two months ago. But, this may be useful for the users who reading this in the future.

Sriraman
  • 7,658
  • 4
  • 40
  • 60
15

Here's how I've fixed this on ubuntu 14.04 when curl was working in php files run through apache, but not when called from the cli.

ssh to your server and cd to /

find / -name 'curl.so'

Run the above find command to locate where the curl binary is hanging out at. If you can't find the file, you might need to install curl and run the find command again.

apt-get install php5-curl

You'll now want to edit the php.ini being used for php files run from the cli (it's different than the one used by apache), and is likely at /etc/php5/cli/php.ini

nano /etc/php5/cli/php.ini

You can also run

php -i | grep 'php.ini'

To get the file path, just to be sure.

In your php.ini file search for [curl] by pressing ctrl + w

You'll now want to add the extension to the file and it should look something like the following, though your path to the curl.so file and such might be a little different:

[curl]
; A default value for the CURLOPT_CAINFO option. This is required to be an
; absolute path.
;curl.cainfo =
extension=/usr/lib/php5/20131226/curl.so

After doing the above, I was able to use curl in php scripts run from the cli.

rmmoul
  • 3,147
  • 3
  • 27
  • 35
  • 1
    +1 upvote really nice informative step by step answer, @mmoul. The contribution I would make is an alternative to `apt-get install php5-curl` or `sudo apt-get install php5-curl` is, if running Ubuntu inside a VM, then ensure is curl added to the config of the VM, e.g. if using a puphpet.com-based config for creating the VM via Vagrant then add curl via the puphpet.com web configurator or edit the config.yaml to list curl in the resulting .zip downloaded from puphpet. Either reprovision or rebuild the VM following this edit (comment part 1 of 2) – therobyouknow Sep 16 '16 at 13:51
  • 1
    (comment part 2 of 2) see also my notes that show where curl goes in the config.yaml here here: https://github.com/guzzle/guzzle/issues/1586#issuecomment-247286744. In my case I needed to add curl to the config of the VM as I described here, in order to get the curl.so Thanks again for your great answer, hope my contribution further enhances it. – therobyouknow Sep 16 '16 at 13:52
4

first find the version of your php cli by:

php -v

for example if it was version 7 then:

sudo apt-cache search php7

this will give you the proper module names for your current version:

php7.0-curl - CURL module for PHP     <---- the name of curl module. 
php7.0-dev - Files for PHP7.0 module development
php7.0-gd - GD module for PHP
php7.0-gmp - GMP module for PHP
php7.0-json - JSON module for PHP
php7.0-ldap - LDAP module for PHP
php7.0-mysql - MySQL module for PHP
.
.
so on

so to add curl support, copy the name of curl module from the list above then do the following:

sudo apt-get install php7.0-curl
Mustafa R.Mahdi
  • 744
  • 7
  • 11
3

If you are using the command-line interface ('cli') for php5, instead of

php -q myfile.php

please use:

php5 -q myfile.php

php5-curl seems to enable the curl module for the cli php5 and not php and both (can) load different configurations and modules.

Rocky Inde
  • 1,511
  • 1
  • 20
  • 27
3

I use ubuntu 14.04 and php 5.3. After upgrading to php 5.6.29 I also has problem with php curl. My directory structure after updating to php 5.6.29:

/etc/php5 - old version (5.3)

/etc/php/5.6 - new version

The next command

sudo apt-get install php5-curl

didn't help (looks like it connects to old php version - 5.3).

I have found next article: php 5.6 for magento

It advice to use command

apt-get -y install php5.6-curl

instead of

apt-get -y install php5-curl

It works for me!

Vaha
  • 2,179
  • 2
  • 17
  • 29
1

The first thing you should always check is your php.ini file. You should have a php.ini file in your web root. Curl is installed by default on most web servers; I haven't found a web server with PHP that hasn't already had curl installed. Its not always enabled, though.

Check your your php.ini file and search for php_curl.dll, it should look like this:

;extension=php_curl.dll

Just remove the semicolon (;) from before "extension" and save the file. It should work right away. According to your phpinfo.php its already installed, so it likely just needs to be enabled.

A similar question can be found here if you're interested: Call to undefined function curl_init()

Community
  • 1
  • 1
JaidynReiman
  • 954
  • 3
  • 11
  • 22
  • 1) The first sentence belongs to a comment. 2) there's no php.ini in the _document root_ (god forbid!) – geomagas Nov 07 '13 at 22:46
  • So I ran a search in my php.ini file and there wasn't a line for `;extension=php_curl.dll` – user2966822 Nov 07 '13 at 22:53
  • The file is located /etc/php5/cli/php.ini – user2966822 Nov 07 '13 at 22:55
  • geomagas, not sure what you're getting at. The document root is the root directory for your public website files. Many servers have a php.ini file in the document root or generate one and place it in the document root. – JaidynReiman Nov 07 '13 at 22:58
  • Also an update, thats the php.ini for the cli, the ini file the browser runs off of is at /usr/local/zend/etc/php.ini, and that did have the `;extension=php_curl.dll` line. I deleted the semicolon and `php -q` is still returning the same error – user2966822 Nov 07 '13 at 23:01
  • user2966822, search for "extension." Then paste "extension=php_curl.dll" on a new line next to other extensions. If you have curl enabled that will work. You probably should NOT overwrite your default php.ini file, though. If you don't have a php.ini file in your document root, paste one there. If you're using a standard server it should work and override the default php.ini file. I don't know what your server is though, so I can't guarantee it. – JaidynReiman Nov 07 '13 at 23:02
  • Your comment updated at the same time as mine. Try copying the file and pasting it into the document root. You shouldn't overwrite the existing php.ini files anyway. – JaidynReiman Nov 07 '13 at 23:06
  • Okay so I tried both of the above, adding the line to my /cli/php.ini and copying my /usr/local/zend/etc/php.ini into the document root and neither worked. I may just try to run it through wget for now – user2966822 Nov 07 '13 at 23:29
  • Thanks so much for the help @jrConway I'll let you know if I can figure it out – user2966822 Nov 07 '13 at 23:29
  • The question is about linux, and there won't be a dll (windows) for curl. – rmmoul Apr 15 '15 at 16:32
0

In case someone reached here to find windows version of running curl.

Open php.ini and remove the ; before extension=php_curl.dll around line 656.

I am pretty much sure what Apache loads is C:\wamp\bin\apache\Apache2.2.17\bin\php.ini therefore you may find curl working from browser.

But when php is run from command line then it may show unknown function curl_init();

Run php -r "echo php_ini_loaded_file();" in the command line to see which ini file is being loaded.

Usually its found inside C:\wamp\bin\php\php5.3.5\php.ini its a different file from what Apache is using. So open it and then remove the ; before extension=php_curl.dll around line 656.

Hope it helps someone.

Paul Bönisch
  • 294
  • 2
  • 10
Pradeep Kumar Kushwaha
  • 2,231
  • 3
  • 23
  • 34