函式名稱:openssl_x509_read()
適用版本:PHP 4 >= 4.0.6, PHP 5, PHP 7
函式說明:openssl_x509_read() 函式從一個PEM編碼的證書中讀取一個X.509證書資源。
語法:resource openssl_x509_read ( mixed $x509certdata )
引數:
- $x509certdata:PEM編碼的證書資料,可以是一個檔案路徑、一個PEM格式的證書字串或一個已經讀取到的檔案資源。
返回值:成功時返回一個X.509證書資源,失敗時返回 FALSE。
示例:
- 從檔案中讀取X.509證書:
$certfile = 'path/to/certificate.pem';
$certdata = file_get_contents($certfile);
$cert = openssl_x509_read($certdata);
if ($cert === false) {
echo "Failed to read the certificate.";
} else {
echo "Certificate read successfully.";
openssl_x509_free($cert); // 釋放資源
}
- 從字串中讀取X.509證書:
$certdata = '-----BEGIN CERTIFICATE-----
...
-----END CERTIFICATE-----';
$cert = openssl_x509_read($certdata);
if ($cert === false) {
echo "Failed to read the certificate.";
} else {
echo "Certificate read successfully.";
openssl_x509_free($cert); // 釋放資源
}
注意:在使用完openssl_x509_read()函式後,應該使用openssl_x509_free()函式釋放資源,以避免記憶體洩漏。