PHP之 实现https ssl证书到期提醒,通过企微发送消息

参考文章
https://blog.51cto.com/17099933344/1935194
https://blog.****.net/m0_37346206/article/details/127333463
https://www.cnblogs.com/tk-bolg/p/18108106

使用的企微接口
https://qyapi.weixin.qq.com/cgi-bin/message/send
在这里插入图片描述

查询 ssl证书到期时间

 // ssl证书即将到期提醒
public function ssl_ts(){
    
    $domain_all = [
            'www.ylhdad.com',
        
        ];
        
    $tz = false;

    foreach ($domain_all as $v){
        $cert_info = $this->get_cert_info($v);

        $validTo_time_t = $cert_info['validTo_time_t'];
        $validTo_time_d = date('Y-m-d H:i:s', $validTo_time_t);   // 过期时间
        
        // 计算剩余天数
        $xc_time = intval( ( $validTo_time_t - time() ) / (60*60*24)  );
        // if($xc_time > 80){
        if($xc_time <= 3){
            $tz=true;
            break;
        }
        
    }

    // if($xc_time <= 3){
    if($tz){
        
        // 替换为你的证书文件路径
        // $certificatePath = '/www/server/panel/vhost/cert/ylhdad.com/privkey.pem';
 
        $data = [
                    "touser"=> "ZhuoZhuoTaoHuaYao|123",
                    // "touser"=> "ZhuoZhuoTaoHuaYao",
                    "msgtype"=> "text",
                    "agentid"=> 1000002,
                    "text"=> [
                        "content"=> "ssl证书即将过期,请及时更新。"
                        // "content"=> "你的快递已到,请携带工卡前往邮件中心领取。\n出发前可查看<a href=\"http://work.weixin.qq.com\">邮件中心视频实况</a>,聪明避开排队。"
                    ],
                    "safe"=> 0,
                    "enable_id_trans"=> 0,
                    "enable_duplicate_check"=> 0,
                    "duplicate_check_interval"=> 1800
                ];
        
       
        
        $qwechat = new Qwechat('ww1', 'k1BuIwtk7p2pdem');
        $token = $qwechat->getAccessToken();
    
        $api = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=".$token;   // 正式域名
            
        $response = $this->post($api, json_encode($data)); 
        Log::info('ssl证书过期,提醒成功'  . json_encode($response) );
        // var_dump('请求返回');
        // var_dump($response);
    }
    Log::info('没有即将ssl证书过期,执行成功' );

}

// 得到ssl证书信息
public function get_cert_info($domain){
    $context = stream_context_create(['ssl' => [
        'capture_peer_cert' => true,
        'capture_peer_cert_chain' => 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;
}

 //请求
public function post($url, $data) {

    $headers = [
        // "Authorization: " . $authorization,
        "Accept: application/json",
        "Content-Type:application/json",
    ];

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);//设置HTTP头
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $res = curl_exec($ch);
    //         $error = curl_error($ch); 
    // var_dump($error);exit(0);

    curl_close($ch);
    return json_decode($res, true);
}
上一篇:【笔记】数据结构12


下一篇:企业微信 标准年级对照表