繁体   English   中英

带有 PHP CURL 的 SSL 证书

[英]SSL Certificate with PHP CURL

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

我收到错误:

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

我已经尝试了在其他类似的 stackoverflow 帖子上发现的所有内容,但没有任何效果,我尝试下载旧的 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.ini 中添加根 CA(签署服务器证书的 CA)

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

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

我在使用 HTTP GET 到远程 https 站点时遇到相同的“SSL 证书问题:无法获取本地颁发者证书结果”错误。 我只需要向 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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM