Core\Http\TlsInfo::expiry
$tlsInfo->expiry(): Core\Time\InstantReturns the time when the certificate of the server a connection went to stops being valid, as a Core\Time\Instant.
Every certificate has an end date. After that time, a client does not accept it, and calls to that server fail until somebody installs a new certificate. expiry() returns the end date as an instant, in whole seconds. You can compare it with another instant, or subtract one from it with since() to get the time that is left.
You get a Core\Http\TlsInfo from Core\Http\Response::tls(). That method returns null for a reply that did not come over TLS, so check for null first.
Good to know: the examples build their TLS connections with Core\Test::tlsSession, so they run without a network. They show the end date, the number of days that are left, and a daily check that warns about certificates that expire soon.
Return value
Returns Core\Time\Instant. The instant when the server's certificate expires. Operators often want a program to check this for the services it calls.
Errors
RuntimeErrorThe server sent no certificate, or its leaf certificate is not X.509 that this method can read.version,cipher,isVerifiedandpeerChainstill work, because they do not read inside a certificate.RuntimeErrorThe certificate has no end date. ItsnotAfteris9999-12-31, which is later than the lastCore\Time\Instant.
Examples
Reading the expiry
<?nvs// Shows how to read when the certificate of the server that sent a reply expires.
// `Core\Test::tlsSession` builds a TLS connection for a test, so the example runs// without a network. `Core\Test::answerHttp` gives a fixed reply that reports it.var $session = Core\Test::tlsSession({ expiry: Core\Time\Instant::fromIso('2031-06-01T12:00:00Z') });Core\Test::answerHttp('https://api.example.com/health', 200, { tls: $session });
?Core\Http\TlsInfo $info = Core\Http\Client::get('https://api.example.com/health')->tls();
// `expiry()` returns a `Core\Time\Instant`. `toIso()` writes it as text.// This prints `2031-06-01T12:00:00Z`.if ($info != null) { echo $info->expiry()->toIso(), "\n";}2031-06-01T12:00:00ZDays until the certificate expires
<?nvs// Shows how to count the days until a certificate expires.
// A TLS connection built for a test, so the example runs without a network.var $session = Core\Test::tlsSession({ expiry: Core\Time\Instant::fromIso('2031-06-11T00:00:00Z') });
// A real program uses `Core\Time::now()`. The example uses a fixed day,// so it prints the same result every time.var $today = Core\Time\Instant::fromIso('2031-06-01T00:00:00Z');
// `since()` returns the time between the two instants as a `Core\Time\Duration`.// One day has 86400 seconds. This prints `days left: 10`.int $seconds = $session->expiry()->since($today)->toSeconds();echo 'days left: ', Core\Math::quotient($seconds, 86400), "\n";days left: 10A renewal warning
<?nvs// Shows a daily check that warns about certificates that expire in the next 30 days.
// Fixed replies with TLS connections built for a test, so the example runs// without a network. The `shop` certificate expires soon.Core\Test::answerHttp('https://api.example.com/health', 200, { tls: Core\Test::tlsSession({ expiry: Core\Time\Instant::fromIso('2031-09-01T00:00:00Z') }),});Core\Test::answerHttp('https://shop.example.com/health', 200, { tls: Core\Test::tlsSession({ expiry: Core\Time\Instant::fromIso('2031-06-15T00:00:00Z') }),});
// A real program uses `Core\Time::now()`. The example uses a fixed day.var $today = Core\Time\Instant::fromIso('2031-06-01T00:00:00Z');var $limit = $today->plus(Core\Time\Duration::days(30));array<string> $hosts = ['api.example.com', 'shop.example.com'];
// `compareTo()` returns a number below 0 when the expiry is before the limit.// This prints `renew soon: shop.example.com expires 2031-06-15T00:00:00Z`.foreach ($hosts as string $host) { ?Core\Http\TlsInfo $info = Core\Http\Client::get('https://' . $host . '/health')->tls(); if ($info != null) { if ($info->expiry()->compareTo($limit) < 0) { echo 'renew soon: ', $host, ' expires ', $info->expiry()->toIso(), "\n"; } }}renew soon: shop.example.com expires 2031-06-15T00:00:00Z