函数名: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 文件中。
请注意,为了简化示例,我们使用了一些默认值,实际使用时可能需要根据具体需求进行调整。