PHP
Chilkat2 PHP
Get the Server Certificate from a TLS Connection
See more Socket/SSL/TLS Examples
Demonstrates Socket.GetServerCert, which copies the certificate presented by the remote TLS server into a Cert object for inspection.
Background. The method returns false when the socket is not connected, is not using TLS, or no server certificate is available. Inspecting the certificate lets an application validate the server's identity.
Chilkat PHP Downloads
<?php
use Chilkat2\Cert;
use Chilkat2\Socket;
$socket = new Socket();
// Connect to the server using TLS.
$bTls = true;
$maxWaitMs = 5000;
try {
$socket->Connect('example.com', 5000, $bTls, $maxWaitMs);
// Copy the certificate presented by the remote TLS server, then inspect it. This can be used to
// validate the server's identity.
$cert = new Cert();
$socket->GetServerCert($cert);
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
echo 'Server certificate subject: ', $cert->SubjectCN, "\n";
echo 'Issued by: ', $cert->IssuerCN, "\n";