php利用OpenSSL写一个网站ssl证书到期检测接口
把下面代码保存为ssl.php
<?php /* php利用OpenSSL写一个网站ssl证书到期检测接口 使用方式:https://www.98qy.com/ssl/ssl.php?domain=90175.com */ function getSSLCertificateExpiry($domain) { // 连接到目标域名的443端口 $get = stream_context_create([ "ssl" => [ "capture_peer_cert" => TRUE, "verify_peer" => FALSE, "verify_peer_name" => FALSE, ], ]); $read = @stream_socket_client( "ssl://{$domain}:443", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $get ); if ($read === FALSE) { return ["error" => "Unable to connect to {$domain}"]; } $cert = stream_context_get_params($read); $certinfo = openssl_x509_parse($cert['options']['ssl']['peer_certificate']); if (!$certinfo) { return ["error" => "Unable to parse the certificate"]; } $validTo = date('Y-m-d H:i:s', $certinfo['validTo_time_t']); return ["domain" => $domain, "valid_to" => $validTo]; } // 用于处理API请求的逻辑 if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['domain'])) { $domain = $_GET['domain']; $result = getSSLCertificateExpiry($domain); header('Content-Type: application/json'); echo json_encode($result); } else { echo json_encode(["error" => "Please provide a domain parameter"]); }
使用方法:https://www.98qy.com/ssl/ssl.php?domain=90175.com
本文由网上采集发布,不代表我们立场,转载联系作者并注明出处:https://www.90175.com/wenku/txtlist_i214v.html