Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if domain has SSL certificate or not?

Tags:

php

https

ssl

Is is possible to get the details like if a domain (say www.example.com) is HTTPS ready?

I need to validate some URLs, whether they have SSL certificate or not. I know, by using $_SERVER['HTTPS'] we can check our server details. but how can I achieve the same for other domains.

Any help would be much appreciated.

like image 710
Krishna Mohan Avatar asked Dec 29 '14 12:12

Krishna Mohan


2 Answers

There's no way to check if the certificate is secure or not using PHP alone without API (green padlock)

But you can use this, which use a API: https://github.com/spatie/ssl-certificate

like image 55
WtrndMOB Avatar answered Sep 20 '22 19:09

WtrndMOB


Finally, I've ended up with the following my code:

$stream = stream_context_create (array("ssl" => array("capture_peer_cert" => true)));
$read = fopen("https://www.example.com", "rb", false, $stream);
$cont = stream_context_get_params($read);
$var = ($cont["options"]["ssl"]["peer_certificate"]);
$result = (!is_null($var)) ? true : false;

If HTTPS is enabled for a domain, var_dump($var) gives the output as shown:

resource(4) of type (OpenSSL X.509) 

If it doesn't exist it returns NULL.

I've checked a few domains. It seems to be working fine. I hope it will help someone.

like image 29
Krishna Mohan Avatar answered Sep 21 '22 19:09

Krishna Mohan