使用我尝试连接的API,需要发送带有请求的ssl密钥。我可以在标准php中做到这一点。当我使用Laravel时,我无法使其正常工作。
$cert = 'app/file_combo.pem';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSLCERT, $cert);
curl_setopt($ch, CURLOPT_SSLCERTPASSWD , $pass);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$response = curl_exec($ch);
没有反应。我认为这是因为我正在交谈的$ url忽略了我。所以我加了:
$info = curl_getinfo($ch);
echo "<pre>";
print_r($info);
响应为:
[upload_content_length] => -1
[starttransfer_time] => 0
[redirect_time] => 0
[redirect_url] =>
[primary_ip] => a.b.c.d
[certinfo] => Array
(
)
证书信息为空。...
所以我添加了:
if(curl_errno($ch)){
echo 'Curl error: ' . curl_error($ch);
}
我看到了
Curl error: could not load PEM client certificate, LibreSSL error error:02FFF002:system library:func(4095):No such file or directory, (no key found, wrong pass phrase, or wrong file format?)
我尝试了许多文件路径,但似乎无法正确处理。 pem文件位于laravel的app目录中。
所以我尝试了Guzzle。
$options = [
'headers' => [
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Connection: Keep-Alive",
"Pragma: no-cache",
"Content-length: ".strlen($xml),
],
'body' => $xml,
'verify' => 'app/file_combo.pem'
];
并得到以下答复:
"message": "SSL CA bundle not found: app/file_combo.pem",
我不在乎是否使用guzzle,我只是想使其正常工作... 再次,在标准php中,curl请求可以很好地工作。