简体   繁体   中英

Installing curl to PHP cli

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!

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.

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.

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.

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!

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()

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM