Skip to content

Instantly share code, notes, and snippets.

@jamesBan
Created June 25, 2021 08:56
Show Gist options
  • Save jamesBan/bf5f2e76405943ec5b588beab34e6b8a to your computer and use it in GitHub Desktop.
Save jamesBan/bf5f2e76405943ec5b588beab34e6b8a to your computer and use it in GitHub Desktop.
检查证书过期
<?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