函数
<?php
function getSllTimeByDomain($domain): array
{
$context = stream_context_create(array("ssl" => array("capture_peer_cert_chain" => true)));
$socket = stream_socket_client("ssl://$domain:443", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context);
$context = stream_context_get_params($socket);
if (!empty($context["options"]["ssl"]["peer_certificate_chain n"])) {
$ret = $context["options"]["ssl"]["peer_certificate_chain n"];
} else if (!empty($context["options"]["ssl"]["peer_certificate_chain"])) {
$ret = $context["options"]["ssl"]["peer_certificate_chain"];
} else {
return [];
}
foreach ($ret as $value) {
//使用openssl扩展解析证书,这里使用x509证书验证函数
$cerInfo = openssl_x509_parse($value);
// if (strpos($cerInfo['name'], $domain)) {
// return [
// 'start' => date("Y-m-d", $cerInfo['validFrom_time_t']),
// 'end' => date("Y-m-d", $cerInfo['validTo_time_t']),
// ];
// }
// 理论上来说,取到第一条的就是正确的证书,再往上应该是该证书的签发机构
// https://blog.csdn.net/zengqiang1/article/details/105754953#:~:text=%E8%AF%81%E4%B9%A6%E9%93%BE%E6%98%AF%E4%B8%80%E4%B8%AA%E6%9C%89%E5%BA%8F,A%E6%98%AF%E5%90%A6%E5%80%BC%E5%BE%97%E4%BF%A1%E4%BB%BB%E3%80%82
return [
'start' => date("Y-m-d", $cerInfo['validFrom_time_t']),
'end' => date("Y-m-d", $cerInfo['validTo_time_t']),
];
}
return [];
}
// 打印测试结果
var_dump(getSllTimeByDomain('www.baidu.com'));
结果输出
array(2) {
["start"]=>
string(10) "2021-07-01"
["end"]=>
string(10) "2022-08-02"
}
本文链接:
http://zimu.devorz.com/archives/19/