我尝试进行cURL身份验证,但是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);