且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 PHP 中使用 CURL 获取 SSL 证书信息?

更新时间:2023-02-24 13:55:04

没有.编辑:CURLINFO_CERTINFO 选项已添加到 PHP 5.3.2.请参阅http://bugs.php.net/49253

No. EDIT: A CURLINFO_CERTINFO option has been added to PHP 5.3.2. See http://bugs.php.net/49253

显然,该信息是由您的代理在响应标头中提供给您的.如果你想依赖它,你可以使用 curl 的 CURLOPT_HEADER 选项true 以在输出中包含标题.

Apparently, that information is being given to you by your proxy in the response headers. If you want to rely on that, you can use curl's CURLOPT_HEADER option to trueto include the headers in the output.

但是,要在不依赖某些代理的情况下检索证书,您必须这样做

However, to retrieve the certificate without relying on some proxy, you must do

<?php
$g = stream_context_create (array("ssl" => array("capture_peer_cert" => true)));
$r = fopen("https://www.google.com/", "rb", false, $g);
$cont = stream_context_get_params($r);
var_dump($cont["options"]["ssl"]["peer_certificate"]);

您可以使用 OpenSSL 扩展操作 $cont["options"]["ssl"]["peer_certificate"] 的值.

You can manipulate the value of $cont["options"]["ssl"]["peer_certificate"] with the OpenSSL extension.

EDIT:此选项更好,因为它实际上并不发出 HTTP 请求并且不需要 allow_url_fopen:

EDIT: This option is better since it doesn't actually make the HTTP request and does not require allow_url_fopen:

<?php
$g = stream_context_create (array("ssl" => array("capture_peer_cert" => true)));
$r = stream_socket_client("ssl://www.google.com:443", $errno, $errstr, 30,
    STREAM_CLIENT_CONNECT, $g);
$cont = stream_context_get_params($r);
var_dump($cont["options"]["ssl"]["peer_certificate"]);