php版https证书过期时间检测/并发送短信给相关技术人员(crontab定时脚本版)

 
'[email protected]',
                            'password'=>'xxxxx',
                            'content'=>$content,
                            'mobile'=>$phone
        );
    $url="http://sms.kingtto.com:9999/sms.aspx?action=send";
    $o='';
    foreach($post_data as $k=>$v){
        //短信内容需要用urlencode编码,否则可能收到乱码
        $o.="{$k}=".urlencode($v).'&';
    }
    $post_data=substr($o,0,-1);
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_POST,1);
    curl_setopt($ch,CURLOPT_HEADER,0);
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$post_data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //如果需要将结果直接返回到变量里,那加上这句。
    $result = curl_exec($ch);
    $arr = json_decode(json_encode(simplexml_load_string($result)),true);
    $new = array();
    foreach ($arr as $k => $v) {
        $new[$k] = strtolower($v);
    }
    unset($result,$arr);
    return $new;


}


$server_name = "baidu.com";
if(!empty($argv[1])) $server_name = $argv[1]; //获取定时脚本传参域名(不带https://)值
$server_name = str_ireplace(['http://','https://'],[],$server_name);
$context = stream_context_create([ 'ssl' => [
  'capture_peer_cert' => true,
  'capture_peer_cert_chain' => true,
 ],
]);


$resource = stream_socket_client("ssl://$server_name:443", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context);
$params = stream_context_get_params($resource);


$cert = $params['options']['ssl']['peer_certificate'];
$cert_info = openssl_x509_parse($cert);


$remain = $cert_info['validTo_time_t'] - time();
$date_num = (int)($remain/86400);
/*if ($remain < 0) {
 * $str = "certification expired\n";
 *} else {
 * $str = "certification will be expired in {$date_num} days\n";
 *}
 */
$msg = "【xxxx】:{$server_name},https证书将于{$date_num}天后过期";
if($date_num<20){//证书过期时间小于20天时发送短信通知,此处用于定时脚本判断使用
	foreach($phone as $v){
		$res = sendSms($v,$msg);
	}
}


?>

 

你可能感兴趣的:(php,web,https)