这是我第一次做这么复杂的事情。我正在做一个项目,在这个项目中,提供API的服务使用证书请求认证。证书在.pfx文件中。
以下是我目前正在使用的代码。
$url = 'https://api.example.com/sign';
$json = '{
"DateAndTimeOfIssue":"2017-08-31T13:28:02.433Z",
"CashierName":"John",
"TransType":"Sale",
"PaymentType":"Card",
"InvoiceNumber":"31082017-2",
"ReferentDocumentNumber":null,
"PAC":"KJ9UG3"
}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSLCERT, dirname(__FILE__) . '/cert/9SSZJACN.pfx');
curl_setopt($ch, CURLOPT_SSLCERTTYPE, "PEM");
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, 'KJ9UG3');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);
curl_setopt($ch, CURLOPT_POST, True);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$result = curl_exec($ch);
if( !$result ){
echo "Curl Error: " . curl_error($ch);
} else {
echo "Success: ". $result;
}
curl_close($ch);
使用上面的代码,我得到的错误信息是
Curl Error: could not load PEM client certificate, OpenSSL error error:0906D06C:PEM routines:PEM_read_bio:no start line, (no key found, wrong pass phrase, or wrong file format?)
我用Postman测试了这个请求,一切都很正常。请告知我在这里做错了什么。
你需要将你的文件转换为PEM文件。
openssl pkcs12 -in client_ssl.pfx -out client_ssl.pem -clcerts
openssl pkcs12 -in client_ssl.pfx -out root.pem -cacerts