0
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.

posted at

updated at

PHPでドメインのSSL証明書の期限日を取得する

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

    /*
     * $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;
    }
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
0
Help us understand the problem. What are the problem?