PHP 致命错误:调用未定义的函数curl_init()

发布于 2024-10-08 09:32:40 字数 1101 浏览 5 评论 0原文

我尝试 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(25

素衣风尘叹 2024-10-15 09:33:57

在 Ubuntu 18.04 上,这两个命令解决了我的这个问题。

sudo apt-get install php5.6-curl //install curl for php 5.6
sudo service apache2 restart //restart apache

On Ubuntu 18.04 these two commands solve my this problem.

sudo apt-get install php5.6-curl //install curl for php 5.6
sudo service apache2 restart //restart apache
醉生梦死 2024-10-15 09:33:57

看来您还没有在服务器上安装 Curl。
检查服务器的PHP版本并运行以下命令来安装curl。

sudo apt-get install php7.2-curl

然后使用以下命令重新启动 apache 服务。

sudo service apache2 restart

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.

sudo apt-get install php7.2-curl

Then restart the apache service by using the following command.

sudo service apache2 restart

Replace 7.2 with your PHP version.

离不开的别离 2024-10-15 09:33:57

要在 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

帥小哥 2024-10-15 09:33:56

许多答案都给出了答案,但没有指出实际问题(这可能是您安装了错误的 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.

も让我眼熟你 2024-10-15 09:33:56

我在 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).

榆西 2024-10-15 09:33:56

函数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

比忠 2024-10-15 09:33:56

对于 linux,您可以通过

sudo apt-get install php5-curl

php.ini 中的 Windows(删除;)来

;extension=php_curl.dll

安装它重新启动 apache 服务器。

For linux you can install it via

sudo apt-get install php5-curl

For Windows(removing the ;) from php.ini

;extension=php_curl.dll

Restart apache server.

帅气称霸 2024-10-15 09:33:55

对于 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.

少跟Wǒ拽 2024-10-15 09:33:55

还有一个答案...
如果您在 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.

以可爱出名 2024-10-15 09:33:55

RusAlex 的答案是正确的,对于 Apache,你必须安装并启用curl 并重新启动你的 apache 服务:

sudo apt-get install php5-curl
sudo service apache2 restart

在我的 带有 nginx 和 php5-fpm 的 Ubuntu 服务器上,我遇到了以下问题。我必须像这样重新启动 nginx 和 php5-fpm:

sudo service nginx restart
sudo service php5-fpm restart

但是我有非工作的 php5-fpm 进程闲置,这显然是 ubuntu 中的一个错误 https://bugs.launchpad.net/ubuntu/+source/php5/+bug/1242376
所以我必须杀死所有空闲的 php5-fpm 进程才能重新启动 php5-fpm 以便实际加载curl模块

sudo kill <Process Id of php5-fpm Process)

RusAlex answer is right in that for Apache you have to install and enable curl and restart your apache service:

sudo apt-get install php5-curl
sudo service apache2 restart

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:

sudo service nginx restart
sudo service php5-fpm restart

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

sudo kill <Process Id of php5-fpm Process)
灼痛 2024-10-15 09:33:55

我也面临这个问题。我的操作系统是Ubuntu 18.04,我的PHP版本是PHP 7.2

这是我解决这个问题的方法:

在 Ubuntu 服务器上安装 CURL:

sudo apt-get install curl

将下面的 PHP CURL 存储库添加到您的源列表中:

sudo add-apt-repository ppa:ondrej/php

刷新您的软件包数据库

sudo apt update

安装 PHP-curl 7.2

sudo apt install php7.2-fpm php7.2-gd php7.2-curl php7.2-mysql php7.2-dev php7.2-cli php7.2-common php7.2-mbstring php7.2-intl php7.2-zip php7.2-bcmath

重新启动 Apache 服务器

sudo systemctl restart apache2

就这样。

我希望这有帮助

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:

sudo apt-get install curl

Add the PHP CURL repository below to your sources list:

sudo add-apt-repository ppa:ondrej/php

Refresh your package database

sudo apt update

Install PHP-curl 7.2

sudo apt install php7.2-fpm php7.2-gd php7.2-curl php7.2-mysql php7.2-dev php7.2-cli php7.2-common php7.2-mbstring php7.2-intl php7.2-zip php7.2-bcmath

Restart Apache Server

sudo systemctl restart apache2

That's all.

I hope this helps

2024-10-15 09:33:55

这对我来说对 raspian 有用:

sudo apt update && sudo apt upgrade
sudo apt install php-curl

最后:

sudo systemctl restart apache2

或:

sudo systemctl restart nginx

This worked for me with raspian:

sudo apt update && sudo apt upgrade
sudo apt install php-curl

finally:

sudo systemctl restart apache2

or:

sudo systemctl restart nginx
悸初 2024-10-15 09:33:54

我在 Windows 平台上使用 PHP7 / Apache 2.4 时遇到此错误。 curl_init 在 CLI 下工作,但不适用于 Apache 2.4。我通过为 libeay32.dll 和 ssleay32.dll 添加 LoadFile 指令解决了这个问题:

LoadFile "C:/path/to/Php7/libeay32.dll"
LoadFile "C:/path/to/Php7/ssleay32.dll"
LoadFile "C:/path/to/Php7/php7ts.dll"
LoadModule php7_module "C:/path/to/Php7/php7apache2_4.dll"

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 adding LoadFile directives for libeay32.dll and ssleay32.dll:

LoadFile "C:/path/to/Php7/libeay32.dll"
LoadFile "C:/path/to/Php7/ssleay32.dll"
LoadFile "C:/path/to/Php7/php7ts.dll"
LoadModule php7_module "C:/path/to/Php7/php7apache2_4.dll"
耶耶耶 2024-10-15 09:33:54

此答案适用于 https 请求:

Curl 没有内置根证书(就像大多数现代浏览器一样)。您需要明确地将其指向 cacert.pem 文件:

  curl_setopt($ch, CURLOPT_CAINFO, '/path/to/cert/file/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:

  curl_setopt($ch, CURLOPT_CAINFO, '/path/to/cert/file/cacert.pem');

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

删除会话 2024-10-15 09:33:54

第 1 步:(

C:/(path to php folder)/php.ini
enable extension=php_curl.dll

删除行末尾的 ;

第 2 步

将其添加到 Apache/conf/httpd.conf 中。 conf(libeay32.dll、ssleay32.dll、libssh2.dll直接在php7文件夹中找到)

# load curl and open ssl libraries
 LoadFile "C:/(path to php folder)/libeay32.dll"
 LoadFile "C:/(path to php folder)/ssleay32.dll"
 LoadFile "C:/(path to php folder)/libssh2.dll"

Step 1 :

C:/(path to php folder)/php.ini
enable extension=php_curl.dll

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

# load curl and open ssl libraries
 LoadFile "C:/(path to php folder)/libeay32.dll"
 LoadFile "C:/(path to php folder)/ssleay32.dll"
 LoadFile "C:/(path to php folder)/libssh2.dll"
迷离° 2024-10-15 09:33:54

(试图让 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.

棒棒糖 2024-10-15 09:33:53

只需添加我对系统中安装了多个版本的 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.

空城缀染半城烟沙 2024-10-15 09:33:53

为了修复这个错误,我做了:

  1. php.ini 文件中,取消注释这一行: extension=php_curl.dll
  2. php.ini 文件中,取消注释此行: extension_dir = "ext"
  3. 我重新启动了 NETBEANS,因为我使用的是内置服务器

To fix this bug, I did:

  1. In php.ini file, uncomment this line: extension=php_curl.dll
  2. In php.ini file, uncomment this line: extension_dir = "ext"
  3. I restarted NETBEANS, as I was using Built-in server
荒路情人 2024-10-15 09:33:52

有这个问题,但在这里找到了答案:https ://askubuntu.com/questions/1116448/cannot-enable-php-curl-on-ubuntu-18-04-php-7-2

必须:

sudo a2dismod php7.0

并且:

sudo a2enmod php7.2

然后:

sudo service apache2 restart

这是在系统升级到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:

sudo a2dismod php7.0

And:

sudo a2enmod php7.2

Then:

sudo service apache2 restart

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

情释 2024-10-15 09:33:52

就我而言,在 Xubuntu 中,我必须安装 libcurl3 libcurl3-dev 库。使用这个命令一切正常:

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

In my case, in Xubuntu, I had to install libcurl3 libcurl3-dev libraries. With this command everything worked:

sudo apt-get install curl libcurl3 libcurl3-dev php5-curl
会傲 2024-10-15 09:33:51

在 Windows 上较新版本的 PHP(例如 PHP 7.x)上,此处先前答案中建议的相应配置行已更改。您需要取消注释(删除行开头的 ;)以下行:

extension_dir = "ext"
extension=curl

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:

extension_dir = "ext"
extension=curl
你是我的挚爱i 2024-10-15 09:33:49

对于Ubuntu:
如有必要,将 extension=php_curl.so 添加到 php.ini 以启用。然后sudo service apache2 restart

通常会自动处理,但在某些情况下 - 例如,在共享开发环境中 - 它可能变成需要手动重新启用。

指纹将匹配所有这三个条件:

  1. php_info 中的curl_init() 调用发生致命错误
  2. ,您将在 php_info 中看到curl 模块作者(表明curl 已安装且可用)
  3. ,您将看到没有curl 配置块(表明curl 没有安装)未加载)

For Ubuntu:
add extension=php_curl.so to php.ini to enable, if necessary. Then sudo 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:

  1. Fatal Error on curl_init() call
  2. in php_info, you will see the curl module author (indicating curl is installed and available)
  3. also in php_info, you will see no curl config block (indicating curl wasn't loaded)
-小熊_ 2024-10-15 09:33:46

我通过以下步骤让它在 ubuntu 16.04 中工作。我的 php 版本是 7.0

sudo apt-get install php7.0-curl

sudo 服务 apache2 重新启动

我从这个链接得到它,请在此处查看更多详细信息

I got it working in ubuntu 16.04 by following steps.My php version was 7.0

sudo apt-get install php7.0-curl

sudo service apache2 restart

i got it from this link check here for more details

新一帅帅 2024-10-15 09:33:43

对于 Windows,如果有人感兴趣,请从 php.ini 中取消注释以下行(通过删除 ;)

;extension=php_curl.dll

重新启动 apache 服务器。

For Windows, if anybody is interested, uncomment the following line (by removing the ;) from php.ini

;extension=php_curl.dll

Restart apache server.

谎言 2024-10-15 09:33:40

您需要安装 php 的 CURL 支持。

在 Ubuntu 中,您可以通过以下方式安装它:

sudo apt-get install php5-curl

如果您使用 apt-get,则无需编辑任何 PHP 配置,但需要重新启动 Apache。

sudo /etc/init.d/apache2 restart

如果您仍然遇到问题,请尝试使用 phpinfo()确保 CURL 列为已安装。 (如果不是,那么您可能需要提出另一个问题,询问为什么您的软件包没有安装。)

PHP CURL 文档

You need to install CURL support for php.

In Ubuntu you can install it via

sudo apt-get install php5-curl

If you're using apt-get then you won't need to edit any PHP configuration, but you will need to restart your Apache.

sudo /etc/init.d/apache2 restart

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文