我尝试进行卷曲身份验证,但是我对SSL证书有问题。
我有此代码:
$username = "123";
$password = "123";
$host = "https://api.shipnow.com.ar/users/authentication";
$headers = array(
'Content-Type:application/json',
'Authorization: Basic '. base64_encode("$username:$password") // <---
);
$process = curl_init($host);
curl_setopt($process, CURLOPT_HTTPHEADER, $headers);
curl_setopt($process, CURLOPT_HEADER, 1);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_POST, 1);
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($process, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($process, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($process, CURLOPT_CAINFO, '/cacert.pem');
curl_setopt($process, CURLOPT_CAPATH, '/cacert.pem');
$return = curl_exec($process);
$error = curl_error($process);
curl_close($process);
var_dump($error);
但是回到我的:
错误设置证书验证位置: Cafile:/cacert.pem capath:/cacert.pem
我从此链接下载cacert.pem:
https://curl.haxx.se/docs/caextract.html
我将其放在我的项目文件夹中,旁边是index.php
有人知道如何解决问题吗?
问:
也许不存在文件。您可以尝试此代码并检查文件。
$username = "123";
$password = "123";
$host = "https://api.shipnow.com.ar/users/authentication";
$headers = array(
'Content-Type:application/json',
'Authorization: Basic '. base64_encode("$username:$password") // <---
);
// Check File
if(!file_exists('/cacert.pem')) die('File not exists.');
$process = curl_init($host);
curl_setopt($process, CURLOPT_HTTPHEADER, $headers);
curl_setopt($process, CURLOPT_HEADER, 1);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_POST, 1);
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($process, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($process, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($process, CURLOPT_CAINFO, '/cacert.pem');
curl_setopt($process, CURLOPT_CAPATH, '/cacert.pem');
$return = curl_exec($process);
$error = curl_error($process);
curl_close($process);
var_dump($error);