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

function sendSms($phone,$content,$tag = 2){

$post_data = array('userid'=56xx,

'password'=>'xxxxx',

'content'=>$content,

'mobile'=>$phone

);

$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

$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

foreach($phone as $v){

$res = sendSms($v,$msg);

}

}

  • 发表于:
  • 原文链接http://kuaibao.qq.com/s/20180409G11TCA00?refer=cp_1026
  • 腾讯「云+社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。

扫码关注云+社区

领取腾讯云代金券