Skip to content

Core\Http\TlsInfo::version

$tlsInfo->version(): string

Returns the TLS version of the connection a reply came over, as a string.

TLS is the encryption that https uses. The client and the server agree on a version when they connect. version() returns TLSv1.3 or TLSv1.2, which is how most TLS tools write these versions. TLS 1.3 is the newest version. Some older servers still use TLS 1.2, and a program can check for that before it sends private data.

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 how to read the version, how to accept only TLS 1.3, and how to list the partner servers that still use TLS 1.2.

Return value

Returns string. TLSv1.3 or TLSv1.2, the way TLS tools print it. For a version that this build cannot name, the result is the version's 0x code from the registry.

Examples

Reading the version

<?nvs
// Shows how to read the TLS version of the connection a reply came over.
// `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({ version: 'TLSv1.3' });
Core\Test::answerHttp('https://api.example.com/health', 200, { body: 'ok', tls: $session });
Core\Http\Response $response = Core\Http\Client::get('https://api.example.com/health');
// `tls()` is `null` for a reply that did not come over TLS, so the program checks first.
// `version()` returns the version as a string. This prints `TLSv1.3`.
?Core\Http\TlsInfo $info = $response->tls();
if ($info != null) {
echo $info->version(), "\n";
}
Output
TLSv1.3

Requiring tls 1 3

<?nvs
// Shows a check that accepts a connection only when it uses TLS 1.3.
class Policy
{
// Returns `true` when the connection uses TLS 1.3, the newest version.
public static function isModern(Core\Http\TlsInfo $info): bool
{
return $info->version() == 'TLSv1.3';
}
}
// Two TLS connections built for a test: one with TLS 1.3 and one with TLS 1.2.
array<Core\Http\TlsInfo> $sessions = [
Core\Test::tlsSession({ version: 'TLSv1.3' }),
Core\Test::tlsSession({ version: 'TLSv1.2' }),
];
// This prints `TLSv1.3 accepted` and then `TLSv1.2 too old`.
foreach ($sessions as Core\Http\TlsInfo $info) {
if (Policy::isModern($info)) {
echo $info->version(), " accepted\n";
} else {
echo $info->version(), " too old\n";
}
}
Output
TLSv1.3 accepted
TLSv1.2 too old

Finding partners on tls 1 2

<?nvs
// Shows a report that lists the partner servers that still use TLS 1.2.
// Fixed replies with TLS connections built for a test, so the example runs
// without a network. The payment server still uses TLS 1.2.
Core\Test::answerHttp('https://pay.example.com/status', 200, { tls: Core\Test::tlsSession({ version: 'TLSv1.2' }) });
Core\Test::answerHttp('https://mail.example.com/status', 200, { tls: Core\Test::tlsSession({ version: 'TLSv1.3' }) });
Core\Test::answerHttp('https://maps.example.com/status', 200, { tls: Core\Test::tlsSession({ version: 'TLSv1.3' }) });
array<string> $partners = [
'https://pay.example.com/status',
'https://mail.example.com/status',
'https://maps.example.com/status',
];
// The loop calls each partner and keeps the addresses that did not use TLS 1.3.
array<string> $outdated = [];
foreach ($partners as string $url) {
?Core\Http\TlsInfo $info = Core\Http\Client::get($url)->tls();
if ($info != null) {
if ($info->version() != 'TLSv1.3') {
$outdated[] = $url . ' uses ' . $info->version();
}
}
}
// This prints one line: `https://pay.example.com/status uses TLSv1.2`.
foreach ($outdated as string $line) {
echo $line, "\n";
}
Output
https://pay.example.com/status uses TLSv1.2