Created
June 25, 2021 08:56
-
-
Save jamesBan/bf5f2e76405943ec5b588beab34e6b8a to your computer and use it in GitHub Desktop.
检查证书过期
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function getSslExpireTime(string $url): int | |
{ | |
if(!filter_var($url, FILTER_VALIDATE_URL)) { | |
throw new \RuntimeException('invalidate url:'.$url); | |
} | |
$domain = parse_url($url, PHP_URL_HOST); | |
$context = stream_context_create([ | |
"ssl" => ["capture_peer_cert" => true] | |
]); | |
$client = stream_socket_client( | |
"ssl://$domain:443", | |
$errno, | |
$errStr, | |
5, | |
STREAM_CLIENT_CONNECT, | |
$context | |
); | |
$cert = stream_context_get_params($client); | |
$info = openssl_x509_parse($cert['options']['ssl']['peer_certificate']); | |
return $info['validTo_time_t']; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment