php 实现https Ssl证书到期监听

180it 2023-04-16 AM 805℃ 0条
function index(){
    $domains = [
        'sumubai.cc',
    ];
    foreach($domains as $domain)
    {
        try{
 
            $cert_info = get_cert_info($domain);
            $dn = $cert_info['subject']['CN']; //证书保护域名
            $validFrom_time_t = date('m-d H:i', $cert_info['validFrom_time_t']); //证书开始时间
            $validTo_time_d = date('m-d H:i', $cert_info['validTo_time_t']); //证书结束时间
 
            echo "+-证书保护域名:" . $dn . " -+------------------+\n";
            echo "+-证书开始时间:" . $validFrom_time_t . " -+------------------+\n";
            echo "+-证书结束时间:" . $validTo_time_d . " -+------------------+\n";
 
            echo '';
            // 7天内到期
            if($cert_info['validTo_time_t']-time() < 7*24*60*60)
            {
               // 这里我接入了钉钉通知
               // (new \app\api\controller\DingController)->DingdingGo($domain." 证书到期 ".$validTo_time_d, 1, $domain." 证书到期 ".$validTo_time_d);
                echo "$domain." 证书到期 ".$validTo_time_d, 1, $domain." 证书到期 ".$validTo_time_d\n";
            }
        } catch (Exception $e)
        {
 
        }
    }
 
    die;
 
}
 
function get_cert_info($domain){
 
    set_time_limit(100);
 
    $context = stream_context_create(['ssl' => [
 
    'capture_peer_cert' => true,
    'capture_peer_cert_chain' => true,
 
    'verify_peer' =>  false, // You could skip all of the trouble by changing this to false, but it's WAY uncool for security reasons.
    'cafile' => '/etc/ssl/certs/cacert.pem',
    //'CN_match' => 'example.com', // Change this to your certificates Common Name (or just comment this line out if not needed)
    'ciphers' => 'HIGH:!SSLv2:!SSLv3',
    'disable_compression' => true,
    ],
    ]);
    $client = stream_socket_client("ssl://".$domain.":443", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context);
    if($client==false) {
    return false;
    }
    $params = stream_context_get_params($client);
    $cert = $params['options']['ssl']['peer_certificate'];
    $cert_info = openssl_x509_parse($cert);
    return $cert_info;
}
index();

来源:https://blog.csdn.net/sumubaiblog/article/details/124971999

支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

php 实现https Ssl证书到期监听