How to Check Domain SSL Certificate using PHP Code

Arfan
Mar 16, 2022

--

Here is the complete code where you can check is SSL Certificate is valid an validation date of domain

$orignal_parse = parse_url($url, PHP_URL_HOST); 
$get = stream_context_create(
array(
"ssl" => array(
"capture_peer_cert" => TRUE,
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
)
);
$read = stream_socket_client("ssl://".$orignal_parse.":443", $errno, $errstr,
30, STREAM_CLIENT_CONNECT, $get);
$cert = stream_context_get_params($read);
$certinfo = openssl_x509_parse($cert['options']['ssl']['peer_certificate']);
$valid_from = date(DATE_RFC2822,$certinfo['validFrom_time_t']);
$valid_to = substr(date(DATE_RFC2822,$certinfo['validTo_time_t']) , 0 ,16);
if(!empty($valid_to)) {
echo json_encode(["status" => true, "valid_from" => $valid_from, "valid_to" => $valid_to]);
} else {
echo json_encode(["status" => false]);
}

Above code will return your SSL certificate is valid or not also will return valid from and valid to certificate.

--

--