」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 如何使用 PHP 中的 CURL 檢索 SSL 憑證資訊

如何使用 PHP 中的 CURL 檢索 SSL 憑證資訊

發佈於2024-11-09
瀏覽:695

How to Retrieve SSL Certificate Information Using CURL with PHP

如何在PHP 中使用CURL 獲取SSL 證書信息

在PHP 中使用SSL 證書時,通常需要訪問有關證書的信息,例如發行人、主題和到期日期。 Stream_context_create() 函數可用來建立指定要使用的 SSL 憑證的流上下文。然後可以使用stream_context_get_params()函數從流上下文中檢索憑證資訊。

例如,以下程式碼取得Google網站的SSL憑證資訊:

$url = "https://www.google.com";
$context = stream_context_create(array("ssl" => array("capture_peer_cert" => TRUE)));
$read = fopen($url, 'r', false, $context);
$cert = stream_context_get_params($read);
$certinfo = openssl_x509_parse($cert['options']['ssl']['peer_certificate']);
print_r($certinfo);

此程式碼將輸出 Google SSL 憑證的以下資訊:

Array
(
    [name] => /C=US/ST=California/L=Mountain View/O=Google Inc/CN=www.google.com
    [subject] => Array
        (
            [C] => US
            [ST] => California
            [L] => Mountain View
            [O] => Google Inc
            [CN] => www.google.com
        )

    [hash] => dcdd9741
    [issuer] => Array
        (
            [C] => US
            [O] => Google Inc
            [CN] => Google Internet Authority G2
        )

    [version] => 2
    [serialNumber] => 3007864570594926146
    [validFrom] => 150408141631Z
    [validTo] => 150707000000Z
    [validFrom_time_t] => 1428498991
    [validTo_time_t] => 1436223600
    [purposes] => Array
        (
            [1] => Array
                (
                    [0] => 1
                    [1] =>
                    [2] => sslclient
                )

            [2] => Array
                (
                    [0] => 1
                    [1] =>
                    [2] => sslserver
                )

            [3] => Array
                (
                    [0] => 1
                    [1] =>
                    [2] => nssslserver
                )

            [4] => Array
                (
                    [0] =>
                    [1] =>
                    [2] => smimesign
                )

            [5] => Array
                (
                    [0] =>
                    [1] =>
                    [2] => smimeencrypt
                )

            [6] => Array
                (
                    [0] => 1
                    [1] =>
                    [2] => crlsign
                )

            [7] => Array
                (
                    [0] => 1
                    [1] => 1
                    [2] => any
                )

            [8] => Array
                (
                    [0] => 1
                    [1] =>
                    [2] => ocsphelper
                )

        )

    [extensions] => Array
        (
            [extendedKeyUsage] => TLS Web Server Authentication, TLS Web Client Authentication
            [subjectAltName] => DNS:www.google.com
            [authorityInfoAccess] => CA Issuers - URI:http://pki.google.com/GIAG2.crt
OCSP - URI:http://clients1.google.com/ocsp

            [subjectKeyIdentifier] => FD:1B:28:50:FD:58:F2:8C:12:26:D7:80:E4:94:E7:CD:BA:A2:6A:45
            [basicConstraints] => CA:FALSE
            [authorityKeyIdentifier] => keyid:4A:DD:06:16:1B:BC:F6:68:B5:76:F5:81:B6:BB:62:1A:BA:5A:81:2F

            [certificatePolicies] => Policy: 1.3.6.1.4.1.11129.2.5.1

            [crlDistributionPoints] => URI:http://pki.google.com/GIAG2.crl

        )

)

此資訊可用於驗證網站身分並確保憑證有效。

版本聲明 本文轉載於:1729134026如有侵犯,請洽[email protected]刪除
最新教學 更多>

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3