简体   繁体   English

将curl安装到PHP cli

[英]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 我正在尝试设置一个需要curl的cronjob,我直接从crontab调用它

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

The problem is, it looks like the curl module isn't installed for my phpcli. 问题是,看起来我的phpcli没有安装curl模块。

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. 当我运行php -m ,curl模块没有显示出来。 However when I go to the browser and dump the php_info(), the module shows up and says its correctly installed. 但是,当我转到浏览器并转储php_info()时,模块会显示并说明它已正确安装。

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. 另一个踢球者是我一直试图用apt-get安装curl到服务器上(Ubuntu 12.04 php 5.4),它似乎取消了我的PHP,因为它开始只是尝试下载index.php文件,无论我试图在哪里浏览到。

Here are the attempts I've made to install curl that have taken down PHP: 以下是我为安装curl而尝试取消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. 在每次重启apache2服务器之后,我仍然没有骰子,它试图下载文件而不是打开页面。

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? 如何将php5-curl安装到cli中,以便我的服务器可以运行它而不必通过浏览器?

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. 另一种可能性是我可以通过crontab文件中的wget运行cronjobs,但我听说这不是最好的选择,可能不可靠。

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. 以下是我在ubuntu 14.04上修复此问题的方法,当curl在通过apache运行的php文件中工作时,但是当从cli调用时没有。

ssh to your server and cd to / ssh到你的服务器和CD到/

find / -name 'curl.so'

Run the above find command to locate where the curl binary is hanging out at. 运行上面的find命令,找到curl二进制文件挂出的位置。 If you can't find the file, you might need to install curl and run the find command again. 如果找不到该文件,则可能需要安装curl并再次运行find命令。

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 你现在想要编辑用于从cli运行的php文件的php.ini(它与apache使用的不同),可能在/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 在你的php.ini文件中按ctrl + w搜索[curl]

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.so文件的路径可能会有所不同:

[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. 完成上述操作后,我能够在从cli运行的php脚本中使用curl。

If you are using the command-line interface ('cli') for php5 , instead of 如果你使用php5的命令行界面('cli'),而不是

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. php5-curl似乎为cli php5而不是php启用curl模块,并且两者(可以)加载不同的配置和模块。

I use ubuntu 14.04 and php 5.3. 我使用ubuntu 14.04和php 5.3。 After upgrading to php 5.6.29 I also has problem with php curl. 升级到PHP 5.6.29后我也有php curl的问题。 My directory structure after updating to php 5.6.29: 更新到php 5.6.29后的目录结构:

/etc/php5 - old version (5.3) / etc / php5 - 旧版本(5.3)

/etc/php/5.6 - new version /etc/php/5.6 - 新版本

The next command 下一个命令

sudo apt-get install php5-curl

didn't help (looks like it connects to old php version - 5.3). 没有帮助(看起来它连接到旧的PHP版本 - 5.3)。

I have found next article: php 5.6 for magento 我找到了下一篇文章: 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. 你应该经常检查的第一件事是你的php.ini文件。 You should have a php.ini file in your web root. 您的Web根目录中应该有一个php.ini文件。 Curl is installed by default on most web servers; 大多数Web服务器默认安装Curl; I haven't found a web server with PHP that hasn't already had curl installed. 我还没有找到一个没有安装curl的PHP Web服务器。 Its not always enabled, though. 但它并不总是能够启用。

Check your your php.ini file and search for php_curl.dll, it should look like this: 检查你的php.ini文件并搜索php_curl.dll,它应该如下所示:

;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. 根据你已安装的phpinfo.php,所以可能只需要启用它。

A similar question can be found here if you're interested: Call to undefined function curl_init() 如果您感兴趣,可以在此处找到类似的问题: 调用未定义的函数curl_init()

first find the version of your php cli by: 首先找到你的php cli的版本:

php -v

for example if it was version 7 then: 例如,如果它是版本7那么:

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: 所以要添加curl支持,请从上面的列表中复制curl模块的名称,然后执行以下操作:

sudo apt-get install php7.0-curl

In case someone reached here to find windows version of running curl. 万一有人到这里找到运行卷曲的Windows版本。

Open php.ini and remove the ; 打开php.ini并删除; before extension=php_curl.dll around line 656. extension=php_curl.dll第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. 我非常确定Apache加载的是C:\\wamp\\bin\\apache\\Apache2.2.17\\bin\\php.ini因此您可以在浏览器中找到curl。

But when php is run from command line then it may show unknown function curl_init(); 但是当从命令行运行php时,它可能会显示未知函数curl_init();

Run php -r "echo php_ini_loaded_file();" 运行php -r "echo php_ini_loaded_file();" in the command line to see which ini file is being loaded. 在命令行中查看正在加载哪个ini文件。

Usually its found inside C:\\wamp\\bin\\php\\php5.3.5\\php.ini its a different file from what Apache is using. 通常它在C:\\wamp\\bin\\php\\php5.3.5\\php.ini它与Apache正在使用的不同的文件。 So open it and then remove the ; 打开它,然后删除; before extension=php_curl.dll around line 656. 在扩展= p​​hp_curl.dll第656行之前。

Hope it helps someone. 希望它可以帮助某人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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