函数名称:openssl_pkcs12_export()
适用版本:PHP 4 >= 4.0.6, PHP 5, PHP 7
函数描述:openssl_pkcs12_export() 函数将一个PKCS#12格式的证书和私钥导出到一个字符串中。
用法: openssl_pkcs12_export(mixed $x509, string &$out, mixed $priv_key, string $pass [, array $args])
参数说明:
返回值:成功时返回 TRUE,失败时返回 FALSE。
示例:
// 从文件中加载证书和私钥
$certFile = 'cert.pem';
$keyFile = 'key.pem';
$passphrase = 'password';
$cert = openssl_x509_read(file_get_contents($certFile));
$key = openssl_pkey_get_private(file_get_contents($keyFile), $passphrase);
// 导出PKCS#12格式的证书和私钥到字符串
if (openssl_pkcs12_export($cert, $p12, $key, $passphrase)) {
// 保存到文件
file_put_contents('cert.p12', $p12);
echo 'PKCS#12证书导出成功!';
} else {
echo 'PKCS#12证书导出失败!';
}
// 释放资源
openssl_x509_free($cert);
openssl_pkey_free($key);
注意事项: