Sample code for 30+ languages & platforms
PHP Requires Chilkat v11.0.0+

Verify SSL Server Certificate

See more Socket/SSL/TLS Examples

Demonstrates how to connect to an SSL server and verify its SSL certificate.

Chilkat PHP Downloads

PHP
<?php

use Chilkat2\Cert;
use Chilkat2\Socket;

$success = false;

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

$socket = new Socket();

$ssl = true;
$maxWaitMillisec = 20000;

// The SSL server hostname may be an IP address, a domain name,
// or "localhost".
$sslServerHost = 'www.paypal.com';
$sslServerPort = 443;

// Connect to the SSL server:
try {
    $socket->Connect($sslServerHost, $sslServerPort, $ssl, $maxWaitMillisec);
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

$cert = new Cert();

$bExpired = false;
$bRevoked = false;
$bSignatureVerified = false;
$bTrustedRoot = false;

try {
    $socket->GetServerCert($cert);
    $success = true;
} catch (\Chilkat2\Exception $e) {
    $success = false;
}
if ($success) {
    echo "Server Certificate:\n";
    echo 'Distinguished Name: ', $cert->SubjectDN, "\n";
    echo 'Common Name: ', $cert->SubjectCN, "\n";
    echo 'Issuer Distinguished Name: ', $cert->IssuerDN, "\n";
    echo 'Issuer Common Name: ', $cert->IssuerCN, "\n";

    $bExpired = $cert->Expired;
    $bRevoked = $cert->Revoked;
    $bSignatureVerified = $cert->SignatureVerified;
    $bTrustedRoot = $cert->TrustedRoot;

    echo 'Expired: ', ($bExpired ? 'true' : 'false'), "\n";
    echo 'Revoked: ', ($bRevoked ? 'true' : 'false'), "\n";
    echo 'Signature Verified: ', ($bSignatureVerified ? 'true' : 'false'), "\n";
    echo 'Trusted Root: ', ($bTrustedRoot ? 'true' : 'false'), "\n";
}

// Close the connection with the server
// Wait a max of 20 seconds (20000 millsec)
$socket->Close(20000);