PHP客户端验证https证书

发布于 2024-12-08 01:30:05 字数 111 浏览 0 评论 0原文

我需要创建一个 php 来充当客户端并使用 https 下的一些 Web 服务。 我的问题是我还想验证服务器证书。我需要知道我有正确的服务器并且中间没有人充当服务器。 有人可以帮我吗?

谢谢!

I need to create a php that will act as a client and use some web services under https.
My Problem is that I also want to verify the server certificate.I need to know that I have the right server and that there is no one the middle that acts as the server.
Can someone help me please?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

煮酒 2024-12-15 01:30:05

如果您有curl 扩展,则可以将其配置为验证连接时的证书。

http://php.net/manual/en/function.curl-setopt。 php

// As of writing this, Twitter uses Verisign, Google uses Eqifax
$exampleUrl = 'https://twitter.com/'; // Success
$exampleUrl = 'https://google.com/';  // Fails

// create a new CURL resource
$ch = curl_init($exampleUrl);

// enable verification
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);

// list of CAs to trust
// If the remote site has a specific CA, they usually have a .crt
// file on their site you can download.  Or you can export key items from
// some browsers.
// In this example, using: Verisign [1]
curl_setopt($ch, CURLOPT_CAINFO, __DIR__ . '/ca_bundle.crt');
// - or -
curl_setopt($ch, CURLOPT_CAPATH, __DIR__ . '/ca_certs/');

// If the remote site uses basic auth:
curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);

// And a helpful option to enable while debugging
//curl_setopt($ch, CURLOPT_VERBOSE, true);

// defaults to stdout, don't want that for this case.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$page = curl_exec($ch);

[1] http://www.verisign.com/support/verisign-intermediate-ca/extended-validation/apache/

If you have the curl extension, it can be configured to verify a certificate on connection.

http://php.net/manual/en/function.curl-setopt.php

// As of writing this, Twitter uses Verisign, Google uses Eqifax
$exampleUrl = 'https://twitter.com/'; // Success
$exampleUrl = 'https://google.com/';  // Fails

// create a new CURL resource
$ch = curl_init($exampleUrl);

// enable verification
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);

// list of CAs to trust
// If the remote site has a specific CA, they usually have a .crt
// file on their site you can download.  Or you can export key items from
// some browsers.
// In this example, using: Verisign [1]
curl_setopt($ch, CURLOPT_CAINFO, __DIR__ . '/ca_bundle.crt');
// - or -
curl_setopt($ch, CURLOPT_CAPATH, __DIR__ . '/ca_certs/');

// If the remote site uses basic auth:
curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);

// And a helpful option to enable while debugging
//curl_setopt($ch, CURLOPT_VERBOSE, true);

// defaults to stdout, don't want that for this case.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$page = curl_exec($ch);

[1] http://www.verisign.com/support/verisign-intermediate-ca/extended-validation/apache/

情话难免假 2024-12-15 01:30:05

看起来从 Curl 7.10 开始,现在默认都设置为检查:
http://php.net/manual/en/function.curl-setopt。 php


CURLOPT_SSL_VERIFYPEER

FALSE 阻止 cURL 验证对等方的证书。可以使用 CURLOPT_CAINFO 选项指定要验证的备用证书,也可以使用 CURLOPT_CAPATH 选项指定证书目录。

自 cURL 7.10 起默认为 TRUE。从 cURL 7.10 开始安装默认捆绑包。


CURLOPT_SSL_VERIFYHOST

1 检查 SSL 对等证书中是否存在通用名称。 2 检查通用名称是否存在,并验证它是否与提供的主机名匹配。在生产环境中,该选项的值应保持为 2(默认值)。

It looks like as of Curl 7.10, this is all set to be checked by default now:
http://php.net/manual/en/function.curl-setopt.php


CURLOPT_SSL_VERIFYPEER

FALSE to stop cURL from verifying the peer's certificate. Alternate certificates to verify against can be specified with the CURLOPT_CAINFO option or a certificate directory can be specified with the CURLOPT_CAPATH option.

TRUE by default as of cURL 7.10. Default bundle installed as of cURL 7.10.


CURLOPT_SSL_VERIFYHOST

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. In production environments the value of this option should be kept at 2 (default value).

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