带有 PHP CURL 的 SSL 证书


SSL Certificate with PHP CURL

我正在使用curl通过https将xml文件发送到rightmove API - 他们为我提供了所有证书。

我收到错误:

60SSL 证书问题:无法获取本地颁发者 证书结果 =

我已经尝试了我在所有其他类似的堆栈溢出帖子上找到的所有内容,但没有任何效果,我尝试下载旧的 cacert.pem 并在 php 中更改文件.ini - 我安装了我的个人信息文件并在浏览器和本地机器上创建了一个证书,没有任何内容可以消除错误 60。

这是我的PHP:

<?php
  $xml = file_get_contents("myxml.xml");
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  curl_setopt ($ch, CURLOPT_CAINFO, dirname(__FILE__).''mypem.pem');
  curl_setopt($ch, CURLOPT_URL, "https://adfapi.adftest.rightmove.com/v1/property/sendpropertydetails");
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
  curl_setopt($ch, CURLOPT_REFERER, 'https://adfapi.adftest.rightmove.com/v1/property/sendpropertydetails');
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt ($ch, CURLOPT_VERBOSE , 1);
  $ch_result = curl_exec($ch);
print curl_errno($ch);
print curl_error($ch);
  echo "Result = ".$ch_result;
  curl_close($ch);
?>

这让我砰

对于我的具体情况,我需要添加密钥文件,sslcert和证书密码。

   //$xml = file_get_contents("thexmlfile.xml");
  $xml= $propertyXml->asXML();
  $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_CAINFO, getcwd() . ''pemfile.pem');
  curl_setopt($ch, CURLOPT_URL, "https://adfapi.adftest.rightmove.com/v1/property/sendpropertydetails");
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_SSH_PRIVATE_KEYFILE, getcwd() . ''myjks.jks');
  curl_setopt($ch, CURLOPT_SSLCERT, getcwd() . ''pemfile.pem');
  curl_setopt($ch, CURLOPT_SSLCERTPASSWD, "thesslpassword");
  curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
  curl_setopt($ch, CURLOPT_REFERER, "https://adfapi.adftest.rightmove.com/v1/property/sendpropertydetails");
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt ($ch, CURLOPT_VERBOSE , 1);
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  $ch_result = curl_exec($ch);
print curl_errno($ch);
print curl_error($ch);
  echo "Result = ".$ch_result;
  curl_close($ch);

它失败,因为 curl 无法验证服务器提供的证书。

有两个选项可以使它工作:

1 允许 curl 建立不安全的连接,即 curl 不验证证书。

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

2 在 php 中添加根 CA(签署服务器证书的 CA).ini

curl.cainfo=/path/to/cacert.pem

您应该使用选项 2,因为这是确保您连接到安全 ftp 服务器的选项。

我遇到了相同的"SSL证书问题:无法获取本地颁发者证书结果"错误,HTTP GET到远程https站点。 我只需要向 php curl 提供 CA 证书,即CURLOPT_CAINFO:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CAINFO, "/path/to/CA_Bundle.crt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($ch);
if (curl_errno($ch)) {
  $error_msg = curl_error($ch);
  print "curl_error: $error_msg <br>";
} else {
  print "output: $output<br>";
}
curl_close($ch);