带SSL的cURL返回我验证位置

时间:2017-11-28 15:58:23

标签: php ssl curl

我尝试进行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旁边

任何人都知道如何解决这个问题?

此致

1 个答案:

答案 0 :(得分:0)

也许文件不存在。你可以尝试这个代码并检查文件。

$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);