PHP 致命错误:调用未定义的函数curl_init()
我尝试 POST 请求中的 PHP Post 请求 认为它可能有用为我。我的代码如下:
$sub_req_url = "http://localhost/index1.php";
$ch = curl_init($sub_req_url);
$encoded = '';
// include GET as well as POST variables; your needs may vary.
foreach($_GET as $name => $value) {
$encoded .= urlencode($name).'='.urlencode($value).'&';
}
foreach($_POST as $name => $value) {
$encoded .= urlencode($name).'='.urlencode($value).'&';
}
// chop off last ampersand
$encoded = substr($encoded, 0, strlen($encoded)-1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_exec($ch);
curl_close($ch);
来自 index.php
文件,index2.php
是同一目录中的另一个文件,当我打开页面时,我在我的页面中收到以下错误error.log
文件:
[Sat Dec 18 15:24:53 2010] [error] [client ::1] PHP 致命错误:在第 5 行 /var/www/testing1/index.php 中调用未定义的函数curl_init()
我想要做什么是有一个发送邮寄请求的预订表格。然后我想处理邮政值并再次发送邮政请求到贝宝。
I try PHP Post Request inside a POST Request thinking it might be useful for me. My code is given below:
$sub_req_url = "http://localhost/index1.php";
$ch = curl_init($sub_req_url);
$encoded = '';
// include GET as well as POST variables; your needs may vary.
foreach($_GET as $name => $value) {
$encoded .= urlencode($name).'='.urlencode($value).'&';
}
foreach($_POST as $name => $value) {
$encoded .= urlencode($name).'='.urlencode($value).'&';
}
// chop off last ampersand
$encoded = substr($encoded, 0, strlen($encoded)-1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_exec($ch);
curl_close($ch);
from the index.php
file and index2.php
is another file in the same directory and when I open the page I get the following error in my error.log
file:
[Sat Dec 18 15:24:53 2010] [error] [client ::1] PHP Fatal error: Call to undefined function curl_init() in /var/www/testing1/index.php on line 5
What I want to do is to have a reservation form that send post request. Then I want to process post values and send again the post request to paypal.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(25)
在 Ubuntu 18.04 上,这两个命令解决了我的这个问题。
On Ubuntu 18.04 these two commands solve my this problem.
看来您还没有在服务器上安装 Curl。
检查服务器的PHP版本并运行以下命令来安装curl。
然后使用以下命令重新启动 apache 服务。
将 7.2 替换为您的 PHP 版本。
Seems you haven't installed the Curl on your server.
Check the PHP version of your server and run the following command to install the curl.
Then restart the apache service by using the following command.
Replace 7.2 with your PHP version.
要在 Ubuntu 上安装最新版本的 php-curl,请使用以下命令:
sudo apt-get install php-curl -y
To install the last version of php-curl on Ubuntu, use this:
sudo apt-get install php-curl -y
许多答案都给出了答案,但没有指出实际问题(这可能是您安装了错误的 PHP 版本!),所以我会在这里重复一遍,希望能做一个更好的工作:
如果您使用的是 Linux 系统(对于 Windows,请参阅其他答案),并且即使在重新启动 Web 服务后,安装“php-curl”的建议也不起作用,那么您的 Web 浏览器可能是不使用最新的 PHP 版本 - 在这种情况下,您需要安装旧版本的 php-curl 库。
要检查您正在运行的 PHP 版本,请访问网站上包含
或检查服务器的设置以查看它加载的 PHP 版本。
例如,如果您发现 Web 服务器使用 PHP 7.4,则使用以下命令安装匹配的库:
sudo apt-get install php7.4-curl
。然后重新启动网络服务器并查看是否有效。Many answers kind of give the answer without pointing your nose at the actual problem (which may be that you're installing the wrong version of PHP!), so I'll repeat it here, hopefully doing a better job:
If you're on a Linux system (for Windows see other answers), and the suggestion to install "php-curl" didn't work, even after restarting the web service, then it might be that your web browser is not using the latest PHP version - and in that case you need to install an older version of the php-curl libs.
To check which PHP version you're running, either visit a page on the website that contains
<?php phpinfo(); ?>
or check your server's settings to see which PHP version it loads.For instance, if you find out that the web server uses PHP 7.4, then install the matching libs with:
sudo apt-get install php7.4-curl
. Then restart the web server and see if that works.我在 Windows 中也遇到了类似的问题。最后错误是因为 php.ini 文件。在我下载的 php 中有 2 个文件 php.ini-development 和 php.ini-product,但我没有 php.ini,所以我只是将其中之一转换为简单的 .ini 文件和未注释的 php-curl 扩展名其中的.dll(许多教程中都描述了解决此问题的方法)。
I had a similar problem in windows. In the end error was because of php.ini file. In php that I downloaded were 2 files php.ini-development and php.ini-production, but I did not have php.ini, so I just converted one of them to simple .ini file and un-commented extension of php-curl.dll in it(it is described in many tutorials to solve this problem).
函数curl_int();
导致服务器错误,安装 sudo apt-get install php5-curl
重新启动 apache2 服务器..它会像魅力一样工作
function curl_int();
cause server error,install sudo apt-get install php5-curl
restart apache2 server .. it will work like charm
对于 linux,您可以通过
php.ini 中的 Windows(删除;)来
安装它重新启动 apache 服务器。
For linux you can install it via
For Windows(removing the ;) from php.ini
Restart apache server.
对于 PHP 7 和 Windows x64
libeay32.dll、libssh2.dll 和 ssleay32.dll 不应位于 apache/bin 中,而应仅存在于 php 目录中,并在系统环境变量中添加 php 目录。这对我有用。
显然,在 php.ini 中,您还应该启用 php_curl.dll。
For PHP 7 and Windows x64
libeay32.dll, libssh2.dll and ssleay32.dll should not be in apache/bin and should only exist in php directory and add php directory in system environment variable. This work for me.
Obvisouly in php.ini you should have enable php_curl.dll as well.
还有一个答案...
如果您在 2020 年 10 月登陆这里,因为命令行 (CLI) 上的 PHP 已停止工作,您猜怎么着……一些升级会悄悄地将您转移到不同/更新版本的 PHP,而无需询问!
跑步:
php --version
您可能会惊讶地发现 CLI 正在运行的版本。然后运行:
ll /usr/bin/php
你可能会惊讶地发现它链接到了哪里。直接调用 PHP 二进制文件时,最好引用您想要的 PHP 的特定版本,而不是符号链接。
示例:
/usr/bin/php7.3
将为您提供您想要的确切版本。您不能信任 /usr/bin/php 甚至只是输入 php,因为升级可能会默默地切换您的版本。Yet another answer ...
If you land here in Oct 2020 because PHP on the command line (CLI) has stopped working, guess what ... some upgrades will move you to a different/newer version of PHP silently, without asking!
Run:
php --version
and you might be surprised to see what version the CLI is running.Then run:
ll /usr/bin/php
and you might be surprised to see where this is linking to.It's best to reference the SPECIFIC version of PHP you want when calling the PHP binary directly and not a symbolic link.
Example:
/usr/bin/php7.3
will give you the exact version you want. You can't trust /usr/bin/php or even just typing php because an upgrade might switch versions on you silently.RusAlex 的答案是正确的,对于 Apache,你必须安装并启用curl 并重新启动你的 apache 服务:
在我的 带有 nginx 和 php5-fpm 的 Ubuntu 服务器上,我遇到了以下问题。我必须像这样重新启动 nginx 和 php5-fpm:
但是我有非工作的 php5-fpm 进程闲置,这显然是 ubuntu 中的一个错误 https://bugs.launchpad.net/ubuntu/+source/php5/+bug/1242376
所以我必须杀死所有空闲的 php5-fpm 进程才能重新启动 php5-fpm 以便实际加载curl模块
RusAlex answer is right in that for Apache you have to install and enable curl and restart your apache service:
On my Ubuntu Server with nginx and php5-fpm I however ran into the following problem. I had to restart nginx and php5-fpm like so:
But I had non-working php5-fpm processes hanging around, which apparently is a bug in ubuntu https://bugs.launchpad.net/ubuntu/+source/php5/+bug/1242376
So I had to kill all idle php5-fpm processes to able to restart php5-fpm so that the curl module is actually loaded
我也面临这个问题。我的操作系统是Ubuntu 18.04,我的PHP版本是PHP 7.2。
这是我解决这个问题的方法:
在 Ubuntu 服务器上安装 CURL:
将下面的 PHP CURL 存储库添加到您的源列表中:
刷新您的软件包数据库
安装 PHP-curl 7.2
重新启动 Apache 服务器
就这样。
我希望这有帮助
I also faced this issue. My Operating system is Ubuntu 18.04 and my PHP version is PHP 7.2.
Here's how I solved it:
Install CURL on Ubuntu Server:
Add the PHP CURL repository below to your sources list:
Refresh your package database
Install PHP-curl 7.2
Restart Apache Server
That's all.
I hope this helps
这对我来说对 raspian 有用:
最后:
或:
This worked for me with raspian:
finally:
or:
我在 Windows 平台上使用 PHP7 / Apache 2.4 时遇到此错误。
curl_init
在 CLI 下工作,但不适用于 Apache 2.4。我通过为 libeay32.dll 和 ssleay32.dll 添加LoadFile
指令解决了这个问题:I got this error using PHP7 / Apache 2.4 on a windows platform.
curl_init
worked from CLI but not with Apache 2.4. I resolved it by addingLoadFile
directives for libeay32.dll and ssleay32.dll:此答案适用于 https 请求:
Curl 没有内置根证书(就像大多数现代浏览器一样)。您需要明确地将其指向 cacert.pem 文件:
如果没有此文件,curl 无法验证通过 ssl 发回的证书。每次在curl 中使用SSL 时都可以使用相同的根证书文件。
您可以在此处获取 cacert.pem 文件: http://curl.haxx.se/docs/ caextract.html
参考 PHP cURL 不适用于 HTTPS
This answer is for https request:
Curl doesn't have built-in root certificates (like most modern browser do). You need to explicitly point it to a cacert.pem file:
Without this, curl cannot verify the certificate sent back via ssl. This same root certificate file can be used every time you use SSL in curl.
You can get the cacert.pem file here: http://curl.haxx.se/docs/caextract.html
Reference PHP cURL Not Working with HTTPS
第 1 步:(
删除行末尾的
;
)第 2 步:
将其添加到
Apache/conf/httpd.conf 中。 conf
(libeay32.dll、ssleay32.dll、libssh2.dll直接在php7文件夹中找到)Step 1 :
(remove the
;
at the end of the line)Step 2 :
Add this to
Apache/conf/httpd.conf
(libeay32.dll, ssleay32.dll, libssh2.dll find directly in php7 folder)(试图让 Curl 在 Windows 上通过 PHP 和 Apache 工作...)
我不断收到错误消息:
调用未定义的函数“curl_init()”
我确保我已在 php.ini 文件中使用此行启用了curl:
extension=php_curl.dll
我确保extension_dir变量设置正确,如下所示:
extension_dir = "ext"
我正在执行论坛上其他人所说的所有操作,但在我对 phpinfo() 的调用中没有出现curl,并且我不断收到上面的相同错误。
最后我发现Apache默认在C:\Windows文件夹中查找php.ini。我一直在更改 PHP 安装文件夹中的 php.ini 。将 php.ini 复制到 C:\Windows 后,一切正常。
我花了很长时间才弄清楚这一点,所以我想我会发帖以防它对其他人有帮助。
(Trying to get Curl working via PHP and Apache on Windows...)
I kept getting an error saying:
Call to undefined function 'curl_init()'
I made sure I had enabled curl with this line in my php.ini file:
extension=php_curl.dll
I made sure the extension_dir variable was being set properly, like this:
extension_dir = "ext"
I was doing everything everyone else said on the forums and curl was not showing up in my call to phpinfo(), and I kept getting that same error from above.
Finally I found out that Apache by default looks for php.ini in the C:\Windows folder. I had been changing php.ini in my PHP installation folder. Once I copied my php.ini into C:\Windows, everything worked.
Took me forever to figure that out, so thought I'd post in case it helps someone else.
只需添加我对系统中安装了多个版本的 PHP 的情况的回答,并且您确定您已经安装了
php-curl
软件包,但 Apache 仍然为您提供相同的信息错误。curl_init() 未定义即使 PHP 7 中启用了 php-curl。
Just adding my answer for the case where there are multiple versions of PHP installed in your system, and you are sure that you have already installed the
php-curl
package, and yet Apache is still giving you the same error.curl_init() undefined even if php-curl is enabled in Php 7.
为了修复这个错误,我做了:
php.ini
文件中,取消注释这一行:extension=php_curl.dll
php.ini
文件中,取消注释此行:extension_dir = "ext"
To fix this bug, I did:
php.ini
file, uncomment this line:extension=php_curl.dll
php.ini
file, uncomment this line:extension_dir = "ext"
有这个问题,但在这里找到了答案:https ://askubuntu.com/questions/1116448/cannot-enable-php-curl-on-ubuntu-18-04-php-7-2
必须:
并且:
然后:
这是在系统升级到Ubuntu 的下一个版本。我发现的所有其他答案都是陈旧的,因为他们中的大多数人指出,PPA 上的证书显然很糟糕,但可能无论如何都不起作用。显然,真正的问题是禁用旧版本的 php。
在这里找到解决方案:
https://askubuntu .com/questions/1116448/cannot-enable-php-curl-on-ubuntu-18-04-php-7-2
Had this problem but found the answer here: https://askubuntu.com/questions/1116448/cannot-enable-php-curl-on-ubuntu-18-04-php-7-2
Had to:
And:
Then:
This was after a system upgrade to next version of Ubuntu. All the other answers I found were stale, due to a bad cert apparently on the PPA most of them pointed out, but would probably not have worked anyway. The real issue was disabling the old versions of php, apparently.
Found the solution here:
https://askubuntu.com/questions/1116448/cannot-enable-php-curl-on-ubuntu-18-04-php-7-2
就我而言,在 Xubuntu 中,我必须安装 libcurl3 libcurl3-dev 库。使用这个命令一切正常:
In my case, in Xubuntu, I had to install libcurl3 libcurl3-dev libraries. With this command everything worked:
在 Windows 上较新版本的 PHP(例如 PHP 7.x)上,此处先前答案中建议的相应配置行已更改。您需要取消注释(删除行开头的 ;)以下行:
On newer versions of PHP on Windows, like PHP 7.x, the corresponding configuration lines suggested on previous answers here, have changed. You need to uncomment (remove the ; at the beginning of the line) the following line:
对于Ubuntu:
如有必要,将
extension=php_curl.so
添加到 php.ini 以启用。然后sudo service apache2 restart
通常会自动处理,但在某些情况下 - 例如,在共享开发环境中 - 它可能变成需要手动重新启用。
指纹将匹配所有这三个条件:
For Ubuntu:
add
extension=php_curl.so
to php.ini to enable, if necessary. Thensudo service apache2 restart
this is generally taken care of automatically, but there are situations - eg, in shared development environments - where it can become necessary to re-enable manually.
The thumbprint will match all three of these conditions:
我通过以下步骤让它在 ubuntu 16.04 中工作。我的 php 版本是 7.0
我从这个链接得到它,请在此处查看更多详细信息
I got it working in ubuntu 16.04 by following steps.My php version was 7.0
i got it from this link check here for more details
对于 Windows,如果有人感兴趣,请从 php.ini 中取消注释以下行(通过删除 ;)
重新启动 apache 服务器。
For Windows, if anybody is interested, uncomment the following line (by removing the ;) from php.ini
Restart apache server.
您需要安装 php 的 CURL 支持。
在 Ubuntu 中,您可以通过以下方式安装它:
如果您使用 apt-get,则无需编辑任何 PHP 配置,但需要重新启动 Apache。
如果您仍然遇到问题,请尝试使用 phpinfo()确保 CURL 列为已安装。 (如果不是,那么您可能需要提出另一个问题,询问为什么您的软件包没有安装。)
PHP CURL 文档。
You need to install CURL support for php.
In Ubuntu you can install it via
If you're using apt-get then you won't need to edit any PHP configuration, but you will need to restart your Apache.
If you're still getting issues, then try and use phpinfo() to make sure that CURL is listed as installed. (If it isn't, then you may need to open another question, asking why your packages aren't installing.)
There is an installation manual in the PHP CURL documentation.