PHP 的 cURL:如何通过 HTTPS 连接?

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

PHP's cURL: How to connect over HTTPS?

php

提问by King of Zhopa

I need to do a simple GET request to EC2 Query API with regular URL encoded query string. The protocol is HTTPS. How would I send the request with the help of PHP's cURL.

我需要使用常规 URL 编码的查询字符串对 EC2 查询 API 执行简单的 GET 请求。协议是HTTPS。我将如何在 PHP 的 cURL 的帮助下发送请求。

回答by rogeriopvl

Example:

例子:

$url = "https://example.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);

$result = curl_exec($ch);
curl_close($ch);

print_r($result);

CURLOPT_SSL_VERIFYPEER

CURLOPT_SSL_VERIFYPEER

Check if peer certificate is valid or invalid/expired.

检查对等证书是否有效或无效/过期。

CURLOPT_SSL_VERIFYHOSTquoting from php manual:

CURLOPT_SSL_VERIFYHOST引用自php 手册

1 to check the existence of a common name in the SSL peer certificate. 2 to check the existence of a common name and also verify that it matches the hostname provided.

1 检查 SSL 对等证书中是否存在通用名称。2 检查通用名称是否存在,并验证它是否与提供的主机名匹配。

回答by Pascal MARTIN

Sending a request via curl, to an HTTPS URL, is not that hard by itself, in terms of PHP code.

就 PHP 代码而言,通过 curl 向 HTTPS URL 发送请求本身并不难。

Something like this should do perfectly fine (I just tried this portion of code on my machine, Windows, PHP 5.3):

像这样的事情应该做得很好(我只是在我的机器、Windows、PHP 5.3 上尝试了这部分代码)

$url = 'https://.../...';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);

echo $data;

And it outputs the result fine : the same thing I get in my browser when trying to access the https://URL ; except for the CSS, of course.

它输出的结果很好:当我尝试访问https://URL时,我在浏览器中得到了同样的结果;当然,CSS 除外。


You might want to take a look at the manual page of the curl_setoptfunction : there are a lot of options, and some of those might be useful, in your specific case :-)


您可能想查看该curl_setopt函数的手册页:有很多选项,其中一些可能对您的特定情况有用:-)

Here, I used CURLOPT_SSL_VERIFYPEERand CURLOPT_SSL_VERIFYHOST; not sure you'll need those with Amazon, but I had to use them, else this portion of code didn't work -- but that might be related to the fact that the certificate I'm using is self-signed... Try with and without those, and you'll quickly find out if you need them.

在这里,我使用了CURLOPT_SSL_VERIFYPEERCURLOPT_SSL_VERIFYHOST;不确定你是否需要亚马逊的那些,但我必须使用它们,否则这部分代码不起作用 - 但这可能与我使用的证书是自签名的事实有关......尝试使用和不使用它们,您会很快发现是否需要它们。

回答by Stephen Curran

If you want to configure CURL to blindly accept the certificate you can set the CURLOPT_SSL_VERIFYPEER option to false.

如果要将 CURL 配置为盲目接受证书,可以将 CURLOPT_SSL_VERIFYPEER 选项设置为 false。

$url = 'https://www.example.com/abc';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Blindly accept the certificate
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$response = curl_exec($ch);
curl_close($ch);

var_dump($response);

回答by user234882

You could also use Zend Framework and the cURL adapter to help with this task. Details here

您还可以使用 Zend Framework 和 cURL 适配器来帮助完成此任务。详情在这里

回答by Krishan Liyanaarachchi

This code can use to retrieve https requests

此代码可用于检索 https 请求

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,'https://serverurl.com/');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$data1 = curl_exec($ch);
curl_close($ch);