我正在尝试使用 Curl
收集一些数据,连接到一些外部公司提供的服务。他们除了解决自身问题外,还向我发送了建立连接所需的 p12
证书文件。
当我尝试将它与 curl
一起使用时,出现以下错误:
#58: not supported file type 'P12' for certificate
到目前为止,我已经尝试更新 curl 和 php-curl。什么都没有改变。
我的代码:
...
curl_setopt($ch, CURLOPT_SSLCERT, 'cert_path');
curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'P12');
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, 'my_pass');
...
有趣的是,这段代码可以在我们的生产环境中运行,但在我的本地机器 (Linux Mint 16) 上却无法运行。
最佳答案
找到解决方案。
最简单的方法是从 .p12
文件中提取 .pem
key 和证书。
例如(在linux
上测试):
openssl pkcs12 -in file.p12 -out file.key.pem -nocerts -nodes
openssl pkcs12 -in file.p12 -out file.crt.pem -clcerts -nokeys
将在当前目录中创建 key /证书对。
现在,使用它:
curl_setopt($ch, CURLOPT_SSLCERT, 'file.crt.pem');
curl_setopt($ch, CURLOPT_SSLKEY, 'file.key.pem');
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, 'pass');
curl_setopt($ch, CURLOPT_SSLKEYPASSWD, 'pass');
其中 pass
是来自 .p12
文件的密码。
关于php - Curl - 无法使用 p12 证书连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24363317/