最新公告
  • 欢迎您光临 我爱模板网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境! 立即加入钻石VIP
  • php怎么看证书信息

    正文概述 管理员   2025-09-21   5

    在 PHP 中,可以使用以下方式来查看证书信息:

    1. 使用openssl_x509_parse()函数

    该函数可以解析 SSL 或 TLS 证书,并返回包含证书信息的关联数组。该数组包含证书的各种细节信息,如版本号、序列号、颁发者、主题、生效时间、过期时间等等。

    示例代码:

    ```php

    <?php

    $data = file_get_contents('/path/to/certificate.crt');

    $certificate = openssl_x509_parse($data);

    echo '<pre>'; print_r($certificate); echo '</pre>';

    ?>

    其中,`$data` 是证书的二进制数据,可以从证书文件中读取。

    2. 使用openssl_x509_read()和openssl_x509_parse()函数

    该方式与第一种方式类似,只不过需要先使用openssl_x509_read()函数读取证书,再使用openssl_x509_parse()函数解析证书。

    示例代码:

    ```php

    <?php

    $data = file_get_contents('/path/to/certificate.crt');

    $certificate = openssl_x509_read($data);

    $info = openssl_x509_parse($certificate);

    echo '<pre>'; print_r($info); echo '</pre>';

    ?>

    需要注意的是,该方式需要先读取证书文件,再读取证书内容。

    3. 使用PHP Stream Context

    使用 PHP Stream Context,可以直接将证书绑定到 HTTP 请求中,从而实现对证书信息的访问。

    示例代码:

    ```php

    <?php

    $context = stream_context_create(array(

    'ssl' => array(

    'verify_peer' => true,

    'verify_peer_name' => true,

    'cafile' => '/path/to/certificate.crt',

    ),

    ));

    $data = file_get_contents('https://www.example.com', false, $context);

    ?>

    其中,`$context` 是一个 PHP Stream Context 上下文,该上下文中包括 SSL 相关的配置信息。在该配置中,使用了`cafile`参数来指定证书的路径。

    使用以上方式可以方便地查看证书信息,从而帮助我们更好地理解证书的细节并进行相关操作。

    在 PHP 中,可以使用 OpenSSL 扩展来解析证书信息。以下是一些示例代码:

    1. 获取证书的公钥:

    ```php

    $certPath = "/path/to/certificate.pem";

    $certContent = file_get_contents($certPath);

    $cert = openssl_x509_read($certContent);

    $pubKey = openssl_pkey_get_public($cert);

    2. 获取证书的颁发机构(CA)信息:

    ```php

    $certPath = "/path/to/certificate.pem";

    $certContent = file_get_contents($certPath);

    $cert = openssl_x509_read($certContent);

    $issuer = openssl_x509_parse($cert)["issuer"];

    3. 获取证书的有效期信息:

    ```php

    $certPath = "/path/to/certificate.pem";

    $certContent = file_get_contents($certPath);

    $cert = openssl_x509_read($certContent);

    $validFrom = date("Y-m-d H:i:s", openssl_x509_parse($cert)["validFrom_time_t"]);

    $validTo = date("Y-m-d H:i:s", openssl_x509_parse($cert)["validTo_time_t"]);

    4. 获取证书的主题信息:

    ```php

    $certPath = "/path/to/certificate.pem";

    $certContent = file_get_contents($certPath);

    $cert = openssl_x509_read($certContent);

    $subject = openssl_x509_parse($cert)["subject"];

    5. 验证证书是否有效:

    ```php

    $certPath = "/path/to/certificate.pem";

    $caPath = "/path/to/ca.pem";

    $certContent = file_get_contents($certPath);

    $cert = openssl_x509_read($certContent);

    $caContent = file_get_contents($caPath);

    $caCert = openssl_x509_read($caContent);

    $ok = openssl_x509_verify($cert, $caCert);

    以上是一些基本的示例代码,你可以根据具体需求进行调整和使用。


    我爱模板网 » php怎么看证书信息

    发表评论

    如需帝国cms功能定制以及二次开发请联系我们

    联系作者
    script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?587cc1e5c052b5b0ce99533beff13c96"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })();

    请选择支付方式

    ×
    支付宝支付
    微信支付
    余额支付
    ×
    微信扫码支付 0 元