2
0

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 2018-02-04

結構古めの記事しかなかったので自分のコピペ用にメモ

    /*
     * $domainからSSLの期限を取得
     */
    public static function getSSLDueDate($domain)
    {
        // 外部接続用のstream_contextを取得
        $stream_context = stream_context_create([
            'ssl' => [
                'capture_peer_cert'     => true
                // エラー回避用
                , 'verify_peer'         => false
                , 'verify_peer_name'    => false
            ]
        ]);
        // 外部接続
        $resource       = stream_socket_client(
            'ssl://' . $domain . ':443'
            , $err_no, $err_str, 5
            , STREAM_CLIENT_CONNECT, $stream_context
        );
        // ドメインの情報を取得
        $context        = stream_context_get_params($resource);
        $parsed         = openssl_x509_parse(
            $context['options']['ssl']['peer_certificate']
        );
        // SSL期限を返す
        return (strpos($parsed['subject']['CN'], $domain) !== false)
            ? date('Y-m-d', $parsed['validTo_time_t']) : null;
    }
2
0
0

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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?