函式名:openssl_x509_export()
適用版本:PHP 4 >= 4.2.0, PHP 5, PHP 7
用法:openssl_x509_export() 函式將一個 X.509 證書匯出為字串。
語法:bool openssl_x509_export ( mixed $x509 , string &$output [, bool $notext = TRUE ] )
引數:
- $x509:要匯出的 X.509 證書。可以是 X.509 證書資源(由 openssl_x509_read() 返回),也可以是 PEM 格式的證書字串。
- $output:匯出的證書字串將被寫入到此變數中。
- $notext(可選):如果設定為 TRUE,則不包含文字描述資訊。
返回值:成功時返回 TRUE,失敗時返回 FALSE。
示例:
<?php
// 從檔案中讀取 X.509 證書
$cert = file_get_contents('path/to/certificate.pem');
// 匯出 X.509 證書為字串
if (openssl_x509_export($cert, $output)) {
echo "證書匯出成功:\n";
echo $output;
} else {
echo "證書匯出失敗";
}
?>
在上面的示例中,我們首先使用 file_get_contents()
函式從檔案中讀取了一個 X.509 證書的內容。然後,我們呼叫 openssl_x509_export()
函式將該證書匯出為字串,並將結果儲存在 $output
變數中。最後,我們檢查匯出是否成功,並列印出匯出的證書字串。
請注意,示例中的檔案路徑 'path/to/certificate.pem'
應該替換為實際的證書檔案路徑。