Php script to check ssl certificate

This worked for me:

Show
$url = "https://www.google.com";
$orignal_parse = parse_url($url, PHP_URL_HOST);
$get = stream_context_create(array("ssl" => array("capture_peer_cert" => 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']);

echo '<pre>';
print_r($certinfo);
echo '</pre>';

fine ssl expire time in this fild

[validTo_time_t] => 1523164135 // expire time.

Convert Date & Time

$valid_from = date(DATE_RFC2822,$certinfo['validFrom_time_t']);
$valid_to = date(DATE_RFC2822,$certinfo['validTo_time_t']);
echo "Valid From: ".$valid_from."<br>";
echo "Valid To:".$valid_to."<br>";

Result:

Valid From: Mon, 09 Oct 2017 07:00:00 +0700 Valid To: Wed, 10 Oct 2018 06:59:59 +0700

sslcheck

A simple php script to check the expiry of SSL certificates.

Requirements

  • PHP (Including CLI support).
  • OpenSSL

This script has been tested only on Ubuntu 16.04+ using PHP7.

Usage

  1. Clone this repo or simply download the sslcheck file.
  2. Ensure the sslcheck file is executable (chmod a+x sslcheck).
  3. Execute the script ./sslcheck www.example.com.

If you can't execute the script directly you may need to envoke the script via php (php sslcheck www.example.com);

You can check multiple domains by listing them all out when running the script:

sslcheck www.google.com www.example.com www.github.com

If a domain is found to have multiple A or AAAA records then each will be checked individually and the IP will be shown alongside the domain in the output.

Usage via Email

The file email-example.sh is a simple script showing how this can be used via email. Simply read the comments at the top of the script and change the configuration variables to set it up. If the mail command is not available on your system you may need to install it (ubuntu: sudo apt-get install mailutils). A good idea would be to set this up as a cron job to send a weekly report.

Oh Dear is the all-in-one monitoring tool for your entire website. We monitor uptime, SSL certificates, broken links, scheduled tasks and more. You'll get a notifications for us when something's wrong. All that paired with a developer friendly API and kick-ass documentation. O, and you'll also be able to create a public status page under a minute. Start monitoring using our free trial now.

Link – Jul 28th 2016

With vanilla PHP it's possible to check of if the SSL certificate of a given site is valid. But it's kinda madness to do it. Let's look at the code required:

<br />// Step 1: downloading the certificate from the site
$streamContext = stream_context_create([
    'ssl' => [
        'capture_peer_cert' => true,
    ],
]);

$client = stream_socket_client(
    "ssl://spatie.be:443",
    $errorNumber,
    $errorDescription,
    $timeout,
    STREAM_CLIENT_CONNECT,
    $streamContext);

$response = stream_context_get_params($client);

$certificateProperties = openssl_x509_parse($response['options']['ssl']['peer_certificate']);

// Step 2: parsing the certificate

/*
* I'm not even going to type out the further code needed.
*
* `$certificateProperties` has two keys `validFrom_time_t` and `validTo_time_t`. 
* Those keys contain the UTC representation of the date.
* You will need to check if the current date is between those dates.
*/ 

What. The. Actual. F. Let's fix this!

We've released a new package named spatie/ssl-certificate that makes checking the SSL certificate of a site laughably easy. Let's take a look at the code:

$certificate = SslCertificate::createForHostName('spatie.be');
$certificate->isValid(); // returns true if the certificate is currently valid

Boom, done.

The package has a few more methods that makes working with an SSL certificate a breeze:

$certificate->getIssuer(); // returns "Let's Encrypt Authority X3"

$certificate->getDomain(); // returns "spatie.be"

//A certificate can cover multiple (sub)domains. Here's how to get them.
$certificate->getAdditionalDomains(); // returns ["spatie.be", "www.spatie.be]

$this->certificate->validFromDate(); // returns an instance of Carbon

$certificate->getExpirationDate(); // returns an instance of Carbon

You can also use isValid to determine if a given domain is covered by the certificate. Of course it'll keep checking if the current datetime is between validFromDate and expirationDate.

$this->certificate->isValid('spatie.be'); // returns true
$this->certificate->isValid('laravel.com'); // returns false

The source code of the package is available on GitHub. My company has made many more PHP framework agnostic, Laravel and JavaScript packages in the past. Take a look at the open source page at our site to see if we've made anything that could be of use to you.

Follow me on Twitter. I regularly tweet out programming tips, and what I myself have learned in ongoing projects.

Every month I send out a newsletter containing lots of interesting stuff for the modern PHP developer.

Expect quick tips & tricks, interesting tutorials, opinions and packages. Because I work with Laravel every day there is an emphasis on that framework.

Rest assured that I will only use your email address to send you the newsletter and will not use it for any other purposes.