我正在编写一个小脚本来显示从特定网站获取的给定证书的信息:
$url = "http://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 "Name: ".$certinfo['name']."<br/>";
echo "Subject: ".$certinfo['subject']['C']." ".$certinfo['subject']['ST']." ".$certinfo['subject']['CN']."<br/>";
echo "Issuer: ".$certinfo['issuer']['C']." ".$certinfo['subject']['O']." ".$certinfo['subject']['CN']."<br/>";
echo "Version: ".$certinfo['version']."<br/>";
echo "Valid from: ".$certinfo['validFrom']." to ".$certinfo['validTo']."<br/>";
有关数组返回内容的完整说明,请访问此处:How to get SSL certificate info with CURL in PHP?
问题是 $certinfo['validFrom']
返回的值如下:
170904000000Z
我不知道它是什么。我想要的是将 $certinfo['validFrom']
和 $certinfo['validTo']
转换为人类日期格式,但我不知道是什么类型这次是,我以为是Unix但是不行,然后我注意到字符串末尾有一个“Z”:
echo "Valid from: ".gmdate("Y-m-d",$certinfo['validFrom'])." to ".gmdate("Y-m-d",$certinfo['validTo'])."<br/>";
返回:有效期从:7385-09-24 到 7683-11-24
这显然是错误的。
我检查了浏览器上的证书和值:
Valid from: 170904000000Z to 180313235959Z
转换好的应该是
Valid from 2017-09-03 to 2017-03-13
谢谢
170904000000Z 不是 ymdhms 吗?世界标准时间 2017 年 9 月 4 日午夜?
我是一名优秀的程序员,十分优秀!