我想从SSL证书文件中获取到期日期。我创建了一个PHP页面,用户可以上传他的SSL证书文件,我将不得不使用PHP获取该文件的到期日期。
答案 0 :(得分:20)
以下代码应该有所帮助:
$url = "https://www.google.com";
$orignal_parse = parse_url($url, PHP_URL_HOST);
$get = stream_context_create(array("ssl" => array("capture_peer_cert" => TRUE)));
$read = stream_socket_client("ssl://".$orignal_parse.":443", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $get);
$cert = stream_context_get_params($read);
$certinfo = openssl_x509_parse($cert['options']['ssl']['peer_certificate']);
echo '<pre>';
print_r($certinfo);
echo '</pre>';
到期日应在$ certinfo ['validTo']字段下。
答案 1 :(得分:9)
这对我有用:
$url = "https://www.google.com";
$orignal_parse = parse_url($url, PHP_URL_HOST);
$get = stream_context_create(array("ssl" => array("capture_peer_cert" => TRUE)));
$read = stream_socket_client("ssl://".$orignal_parse.":443", $errno, $errstr,
30, STREAM_CLIENT_CONNECT, $get);
$cert = stream_context_get_params($read);
$certinfo = openssl_x509_parse($cert['options']['ssl']['peer_certificate']);
echo '<pre>';
print_r($certinfo);
echo '</pre>';
罚款ssl在此fild中过期时间
[validTo_time_t] => 1523164135 // expire time.
转换日期&amp;时间
$valid_from = date(DATE_RFC2822,$certinfo['validFrom_time_t']);
$valid_to = date(DATE_RFC2822,$certinfo['validTo_time_t']);
echo "Valid From: ".$valid_from."<br>";
echo "Valid To:".$valid_to."<br>";
结果:
Valid From:Mon,09 Oct 07:00:00 +0700 有效期:周一,2018年10月10日06:59:59 +0700
答案 2 :(得分:6)
$certpath = "your_certificate.cer";
$certinfo = openssl_x509_parse(file_get_contents($certpath));
if( $certinfo['validFrom_time_t'] > time() || $certinfo['validTo_time_t'] < time() )
print "Certificate is expired.";