php 获取SSL 证书信息5

180it 2021-10-03 PM 885℃ 0条
<?php
$url = "http://www.baidu.com";
/** 
 * Takes a domain name and returns SSL certificate information
 * 
 * @param string $domain_name
 * @return array $certinfo
 */
function getSSL($domain_name)
{
    $errno = 0;
    $errstr = '';
    
    // Set socket connection timeout
    $timeout = 30;
    // Create a stream context to open a ssl socket
    // Set options to get ssl certificate
    // https://www.php.net/manual/en/context.php
    // https://www.php.net/manual/en/context.ssl.php
    $ssl_info = stream_context_create(array(
                                        "ssl" => array(
                                        "capture_peer_cert" => TRUE)));
    $stream = stream_socket_client("ssl://" . $domain_name . ":443", 
                                    $errno, 
                                    $errstr, 
                                    $timeout, 
                                    STREAM_CLIENT_CONNECT, 
                                    $ssl_info);
    if (!$stream) {
        echo "ERROR: $errno - $errstr";
    } else {
        $cert_resource = stream_context_get_params($stream);
        $certificate = $cert_resource['options']['ssl']['peer_certificate'];
        $certinfo = openssl_x509_parse($certificate);
        fclose($stream);
        return $certinfo;
    }
}


print_r(getSSL($url));
?>
支付宝打赏支付宝打赏 微信打赏微信打赏

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

标签: none

php 获取SSL 证书信息5