是否可以从任何网站读取 PHP 中的 SSL 信息?

发布于 2024-09-14 03:51:19 字数 323 浏览 12 评论 0原文

我想知道是否可以使用 PHP 从其他方读取有关其 SSL 证书信息的信息,我多年来一直试图找到它,但没有找到真正的答案。

例如,我在脚本中输入“www.paypal.com”,它将返回以下内容:

  • 授权:VeriSign, Inc
  • 过期:2011 年 2 月 18 日 (18/02/11)
  • 类型:扩展验证
  • 主机:www.paypal.com
  • MD5:a8e7o7a8e9e9
  • SHA1:c2a4a1e4e3a2

并且,无论其他什么都可能获得。我想要 PHP 脚本。

I want to know is it possible to read information from other parties using PHP about their SSL certificate information, I've tried to find about it for ages but there's no real answer that has been found for me.

For example, I input "www.paypal.com" into the script and it will return the following:

  • Authority: VeriSign, Inc
  • Expires: 18th February 2011 (18/02/11)
  • Type: Extended Validation
  • Host: www.paypal.com
  • MD5: a8e7o7a8e9e9
  • SHA1: c2a4a1e4e3a2

And, whatever else is possible to obtain. I would like the script in PHP please.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

行至春深 2024-09-21 03:51:19
<?php
$g = stream_context_create (array("ssl" => array("capture_peer_cert" => true)));
$r = stream_socket_client("ssl://www.google.com:443", $errno, $errstr, 30,
    STREAM_CLIENT_CONNECT, $g);
$cont = stream_context_get_params($r);
print_r( openssl_x509_parse($cont["options"]["ssl"]["peer_certificate"]) );
?>
<?php
$g = stream_context_create (array("ssl" => array("capture_peer_cert" => true)));
$r = stream_socket_client("ssl://www.google.com:443", $errno, $errstr, 30,
    STREAM_CLIENT_CONNECT, $g);
$cont = stream_context_get_params($r);
print_r( openssl_x509_parse($cont["options"]["ssl"]["peer_certificate"]) );
?>
我不在是我 2024-09-21 03:51:19

我编写了一个用于获取 SSL 信息的 PHP 类:

class SSL {

    public $domain, $validFrom, $validTo, $issuer, $validity, $validitytot, $crtValRemaining;

    private static function instantiate($url, $info) {
        $obj = new static;
        $obj->domain = $url;
        $obj->validFrom = $info['validFrom'];
        $obj->validTo = $info['validTo'];
        $obj->issuer = $info['issuer'];
        $obj->validity = $info['validity'];
        $obj->validitytot = $info['validitytot'];
        $obj->crtValRemaining = $info['crtValRemaining'];

        return $obj;
    }

    public static function getSSLinfo($url) {
        $ssl_info = [];
        $certinfo = static::getCertificateDetails($url);
        $validFrom_time_t_m = static::dateFormatMonth($certinfo['validFrom_time_t']);
        $validTo_time_t_m = static::dateFormatMonth($certinfo['validTo_time_t']);

        $validFrom_time_t = static::dateFormat($certinfo['validFrom_time_t']);
        $validTo_time_t = static::dateFormat($certinfo['validTo_time_t']);
        $current_t = static::dateFormat(time());

        $ssl_info['validFrom'] = $validFrom_time_t_m;
        $ssl_info['validTo'] = $validTo_time_t_m;
        $ssl_info['issuer'] = $certinfo['issuer']['O'];

        $ssl_info['validity'] = static::diffDate($current_t, $validTo_time_t)." days";
        $ssl_info['validitytot'] = (static::diffDate($validFrom_time_t, $validTo_time_t)-1).' days';

        $ssl_info['crtValRemaining'] =$certinfo['validTo_time_t'];

        return static::instantiate($url, $ssl_info); // return an object
    }

    private static function getCertificateDetails($url) {
        $urlStr = strtolower(trim($url)); 

        $parsed = parse_url($urlStr);// add http://
        if (empty($parsed['scheme'])) {
            $urlStr = 'http://' . ltrim($urlStr, '/');
        }
        $orignal_parse = parse_url($urlStr, PHP_URL_HOST);
        $get = stream_context_create(array("ssl" => array("capture_peer_cert" => TRUE)));
        $read = stream_socket_client("ssl://".$orignal_parse.":443", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $get);
        $cert = stream_context_get_params($read);
        $certinfo = openssl_x509_parse($cert['options']['ssl']['peer_certificate']);
        return $certinfo;
    }

    private static function dateFormat($stamp) {
        return  strftime("%Y-%m-%d", $stamp);
    }

    private static function dateFormatMonth($stamp) {
        return  strftime("%Y-%b-%d", $stamp);
    }

    private static function diffDate($from, $to) {
        $date1=date_create($from);
        $date2=date_create($to);
        $diff=date_diff($date1,$date2);
        return ltrim($diff->format("%R%a"), "+");
    }

}

EX:

$certInfo = SSL::getSSLinfo('stackoverflow.com'); 
echo $certInfo->validFrom .'<br>';
echo $certInfo->validTo .'<br>';
echo $certInfo->issuer .'<br>';
echo $certInfo->validity .'<br>';
echo $certInfo->validitytot .'<br>';
echo $certInfo->crtValRemaining .'<br>';

[确保您了解 SSL 类中的“实例化”方法]。谢谢...

I have written a PHP class for getting SSL information:

class SSL {

    public $domain, $validFrom, $validTo, $issuer, $validity, $validitytot, $crtValRemaining;

    private static function instantiate($url, $info) {
        $obj = new static;
        $obj->domain = $url;
        $obj->validFrom = $info['validFrom'];
        $obj->validTo = $info['validTo'];
        $obj->issuer = $info['issuer'];
        $obj->validity = $info['validity'];
        $obj->validitytot = $info['validitytot'];
        $obj->crtValRemaining = $info['crtValRemaining'];

        return $obj;
    }

    public static function getSSLinfo($url) {
        $ssl_info = [];
        $certinfo = static::getCertificateDetails($url);
        $validFrom_time_t_m = static::dateFormatMonth($certinfo['validFrom_time_t']);
        $validTo_time_t_m = static::dateFormatMonth($certinfo['validTo_time_t']);

        $validFrom_time_t = static::dateFormat($certinfo['validFrom_time_t']);
        $validTo_time_t = static::dateFormat($certinfo['validTo_time_t']);
        $current_t = static::dateFormat(time());

        $ssl_info['validFrom'] = $validFrom_time_t_m;
        $ssl_info['validTo'] = $validTo_time_t_m;
        $ssl_info['issuer'] = $certinfo['issuer']['O'];

        $ssl_info['validity'] = static::diffDate($current_t, $validTo_time_t)." days";
        $ssl_info['validitytot'] = (static::diffDate($validFrom_time_t, $validTo_time_t)-1).' days';

        $ssl_info['crtValRemaining'] =$certinfo['validTo_time_t'];

        return static::instantiate($url, $ssl_info); // return an object
    }

    private static function getCertificateDetails($url) {
        $urlStr = strtolower(trim($url)); 

        $parsed = parse_url($urlStr);// add http://
        if (empty($parsed['scheme'])) {
            $urlStr = 'http://' . ltrim($urlStr, '/');
        }
        $orignal_parse = parse_url($urlStr, PHP_URL_HOST);
        $get = stream_context_create(array("ssl" => array("capture_peer_cert" => TRUE)));
        $read = stream_socket_client("ssl://".$orignal_parse.":443", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $get);
        $cert = stream_context_get_params($read);
        $certinfo = openssl_x509_parse($cert['options']['ssl']['peer_certificate']);
        return $certinfo;
    }

    private static function dateFormat($stamp) {
        return  strftime("%Y-%m-%d", $stamp);
    }

    private static function dateFormatMonth($stamp) {
        return  strftime("%Y-%b-%d", $stamp);
    }

    private static function diffDate($from, $to) {
        $date1=date_create($from);
        $date2=date_create($to);
        $diff=date_diff($date1,$date2);
        return ltrim($diff->format("%R%a"), "+");
    }

}

EX:

$certInfo = SSL::getSSLinfo('stackoverflow.com'); 
echo $certInfo->validFrom .'<br>';
echo $certInfo->validTo .'<br>';
echo $certInfo->issuer .'<br>';
echo $certInfo->validity .'<br>';
echo $certInfo->validitytot .'<br>';
echo $certInfo->crtValRemaining .'<br>';

[Make sure you understand the "instantiate" method inside SSL class]. Thank you...

凉墨 2024-09-21 03:51:19

PHP 的 OpenSSL 函数如 openssl_x509_parse 应该可以帮助你。

PHP's OpenSSL functions like openssl_x509_parse should help you out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文