php curl_init() 函数不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4477535/
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 13:08:11  来源:igfitidea点击:

curl_init() function not working

phplampp

提问by Santosh Linkha

Hi I tries PHP Post Request inside a POST Requestthinking it might be useful to me and my code is given below

嗨,我在 POST 请求中尝试PHP 发布请求,认为它可能对我有用,下面给出了我的代码

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

form 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

形成 index.php 文件和 index2.php 是同一目录中的另一个文件,当我打开页面时,我的 error.log 文件中出现以下错误

[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 I have a reservation form that send post request and then i want to process post values and send again post request to paypal

我想要做的是我有一个预订表格,发送邮寄请求,然后我想处理邮寄值并再次向贝宝发送邮寄请求

回答by RusAlex

You need to install CURL support for php.

您需要为 php 安装 CURL 支持。

In Ubuntu you can install it via

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

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.

如果您使用的是 apt-get,则不需要编辑任何 PHP 配置,但需要重新启动 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.)

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

There is an installation manual in the PHP CURL documentation.

PHP CURL 文档中有安装手册。

回答by Kanda

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

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

;extension=php_curl.dll

Restart apache server.

重启apache服务器。

回答by Kevin Ard

For Ubuntu: add extension=php_curl.soto php.ini to enable, if necessary. Then sudo service apache2 restart

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

this is generallytaken care of automatically, but there are situations - eg, in shared development environments - where it canbecome 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)
  1. curl_init() 调用的致命错误
  2. 在 php_info 中,您将看到 curl 模块作者(表明 curl 已安装且可用)
  3. 同样在 php_info 中,您将看不到 curl 配置块(表示未加载 curl)

回答by Jose Kj

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

我按照以下步骤在 ubuntu 16.04 中工作。我的 php 版本是 7.0

sudo apt-get install php7.0-curl

sudo service apache2 restart

须藤 apt-get 安装 php7.0-curl

须藤服务 apache2 重启

i got it from this link check here for more details

我是从这个链接得到的,在这里查看更多细节

回答by Hiruna De Alwis

Step 1:

第 1 步

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

(remove the ;at the end of the line)

(删除;行尾的 )

Step 2:

第 2 步

Add this to Apache/conf/httpd.conf(libeay32.dll, ssleay32.dll, libssh2.dll find directly in php7 folder)

将此添加到Apache/conf/httpd.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"

回答by Rajat26

To fix this bug, I did:

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

  1. In php.inifile, uncomment this line: extension=php_curl.dll
  2. In php.inifile, uncomment this line: extension_dir = "ext"
  3. I restarted NETBEANS, as I was using Built-in server
  1. php.ini文件中,取消注释这一行:extension=php_curl.dll
  2. php.ini文件中,取消注释这一行:extension_dir = "ext"
  3. 我重新启动了 NETBEANS,因为我使用的是内置服务器

回答by lightbyte

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

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

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

回答by fishbone

I got this error using PHP7 / Apache 2.4 on a windows platform. curl_initworked from CLI but not with Apache 2.4. I resolved it by adding LoadFiledirectives for libeay32.dll and ssleay32.dll:

我在 Windows 平台上使用 PHP7/Apache 2.4 时遇到此错误。curl_init从 CLI 工作,但不适用于 Apache 2.4。我通过LoadFile为 libeay32.dll 和 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"

回答by Amit Garg

This answer is for https request:

此答案适用于 https 请求:

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 没有内置的根证书(就像大多数现代浏览器一样)。您需要明确地将其指向 cacert.pem 文件:

  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.

没有这个,curl 无法验证通过 ssl 发回的证书。每次在 curl 中使用 SSL 时,都可以使用相同的根证书文件。

You can get the cacert.pem file here: http://curl.haxx.se/docs/caextract.html

您可以在此处获取 cacert.pem 文件:http://curl.haxx.se/docs/caextract.html

Reference PHP cURL Not Working with HTTPS

参考PHP cURL 不适用于 HTTPS

回答by ultrajohn

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-curlpackage, and yet Apache is still giving you the same error.

只需为系统中安装了多个 PHP 版本的情况添加我的答案,并且您确定已经安装了该php-curl软件包,但 Apache 仍然给您相同的错误。

curl_init() undefined even if php-curl is enabled in Php 7.

curl_init() 未定义,即使在 PHP 7 中启用了 php-curl。