33
38

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PHP で外部サーバーの SSL サーバー証明書の有効期限を取得する

Last updated at Posted at 2014-02-27

下記のコードを実行すると qiita.com の SSL サーバー証明書の有効期限を取得することができます。

<?php
$domain_name = 'qiita.com';

$stream_context = stream_context_create(array(
  'ssl' => array('capture_peer_cert' => true)
));
$resource = stream_socket_client(
  'ssl://' . $domain_name . ':443',
  $errno,
  $errstr,
  30,
  STREAM_CLIENT_CONNECT,
  $stream_context
);
$cont = stream_context_get_params($resource);
$parsed = openssl_x509_parse($cont['options']['ssl']['peer_certificate']);

if(strpos($parsed['subject']['CN'], $domain_name) !== false){
  echo date('Y/m/d', $parsed['validTo_time_t']);
}else{
  echo 'not contract.';	
}

実行結果です。

2014/03/21

これを PHPUnit などでテストにして Jenkins で定期実行すれば SSL サーバー証明書の有効期限を監視できますね。

下記を参考にしました。ありがとうございます。
http://stackoverflow.com/questions/3081042/how-to-get-ssl-certificate-info-with-curl-in-php

33
38
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
33
38

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?