函式名:openssl_x509_export_to_file()
適用版本:PHP 4 >= 4.0.6, PHP 5, PHP 7
函式說明:openssl_x509_export_to_file() 函式將 X.509 證書匯出到檔案。
用法:openssl_x509_export_to_file(string $x509, string $outfilename [, bool $notext = true])
引數:
- $x509:要匯出的 X.509 證書。
- $outfilename:匯出的檔名。
- $notext(可選):設定為 true 時,不會在輸出中包含文字資訊,預設為 true。
返回值:成功時返回 true,失敗時返回 false。
示例:
<?php
// 生成一個自簽名的 X.509 證書
$privateKey = openssl_pkey_new(array(
'private_key_bits' => 2048,
'private_key_type' => OPENSSL_KEYTYPE_RSA,
));
$csr = openssl_csr_new(array(
'commonName' => 'example.com',
), $privateKey);
$cert = openssl_csr_sign($csr, null, $privateKey, 365);
// 將證書匯出到檔案
openssl_x509_export_to_file($cert, 'example.crt');
echo 'X.509 證書已成功匯出到 example.crt 檔案。';
?>
在上面的示例中,我們首先使用 openssl_pkey_new() 函式生成一個私鑰,然後使用 openssl_csr_new() 函式生成一個證書籤名請求(CSR),最後使用 openssl_csr_sign() 函式簽署證書。然後,我們使用 openssl_x509_export_to_file() 函式將簽署後的證書匯出到 example.crt 檔案中。
請注意,為了簡化示例,我們使用了一些預設值,實際使用時可能需要根據具體需求進行調整。