将 curl 安装到 PHP cli

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19848055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:08:18  来源:igfitidea点击:

Installing curl to PHP cli

phpubuntucurlcroncommand-line-interface

提问by user2966822

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

当我从浏览器点击 url 时它工作得很好,但是当我运行时

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 -mthe 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.

当我运行php -mcurl 模块没有出现。但是,当我转到浏览器并转储 php_info() 时,该模块会显示并说它已正确安装。

The other kicker is i've been trying to install curl with apt-getonto 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.phpfile wherever I try to browse to.

另一个问题是我一直在尝试将 curl with 安装apt-get到服务器(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-curlto 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!

任何帮助深表感谢。谢谢!

回答by Sriraman

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.

安装后重启服务器。这个回答可能对提问的用户没有用,因为他两个月前问过。但是,这可能对将来阅读本文的用户有用。

回答by rmmoul

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. If you can't find the file, you might need to install curl and run the find command again.

运行上面的 find 命令来定位 curl 二进制文件所在的位置。如果找不到该文件,则可能需要安装 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。

回答by Vaha

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:

我使用 ubuntu 14.04 和 php 5.3。升级到 php 5.6.29 后,我也遇到了 php curl 的问题。更新到 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

apt-get -y install php5.6-curl

instead of

代替

apt-get -y install php5-curl

apt-get -y install php5-curl

It works for me!

这个对我有用!

回答by Rocky Inde

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

如果您使用命令行界面 ('cli') for php5, 而不是

php -q myfile.php

please use:

请用:

php5 -q myfile.php

php5-curlseems to enable the curlmodule for the cli php5and not phpand both (can) load different configurations and modules.

php5-curl似乎curl为 cli启用模块,php5而不是启用模块,php并且两者(都可以)加载不同的配置和模块。

回答by JaidynReiman

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.

您应该始终检查的第一件事是您的 php.ini 文件。您的 Web 根目录中应该有一个 php.ini 文件。Curl 默认安装在大多数 Web 服务器上;我还没有找到一个还没有安装 curl 的带有 PHP 的 Web 服务器。不过,它并不总是启用。

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

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

如果您有兴趣,可以在此处找到类似的问题:Call to undefined function curl_init()

回答by Mustafa R.Mahdi

first find the version of your php cliby:

首先通过以下方式找到您的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

回答by Pradeep Kumar Kushwaha

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

如果有人到达这里找到运行 curl 的 Windows 版本。

Open php.iniand remove the ; before extension=php_curl.dllaround line 656.

打开php.ini并移除 ; 在extension=php_curl.dll第 656 行之前。

I am pretty much sure what Apache loads is C:\wamp\bin\apache\Apache2.2.17\bin\php.initherefore 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();"in the command line to see which ini file is being loaded.

php -r "echo php_ini_loaded_file();"在命令行中运行以查看正在加载哪个 ini 文件。

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

通常它在C:\wamp\bin\php\php5.3.5\php.ini与 Apache 使用的文件不同的文件中找到。所以打开它然后删除; 在第 656 行附近的 extension=php_curl.dll 之前。

Hope it helps someone.

希望它可以帮助某人。