【问题标题】:SSL Certificate with PHP CURL带有 PHP CURL 的 SSL 证书
【发布时间】:2016-07-08 00:14:39
【问题描述】:

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

我收到了错误:

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

我已经尝试了我在所有其他类似的 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);

?>

这让我头疼了好几天,如果能提供任何帮助,我将不胜感激。

【问题讨论】:

  • 尝试设置 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);并删除 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  • @Indrajit 谢谢indrajit,但是api需要认证
  • 大概您在浏览器中没有收到相同的错误。要么你没有更新你的补丁,要么你安装了一个没有为你的平台正确配置的 PHP 版本。
  • 我在本地、我的网络服务器和我的工作服务器上收到此错误。@symcbean,但是你是对的,我可以在浏览器中导航没有问题。
  • 尝试了一整天后仍然无法解决这个问题

标签: php xml ssl curl


【解决方案1】:

对于我的特殊情况,我需要添加密钥文件、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 请求。我也在为 Rightmove 的 Curl Request 苦苦挣扎了几天。
  • 谢谢,这对我有帮助
【解决方案2】:

因为 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 服务器的选项。

【讨论】:

  • 我厌倦了选项 2 与 cacert.pem(我在互联网上从其他帖子中找到的多个)并且它没有改变任何东西。
  • 如果我尝试不验证主机,api当然会返回身份验证失败
  • 当我更改 curl.cainfo 时,我得到这个:60SSL 证书问题:无法获取本地颁发者证书结果 =
  • @GazSmith using curl ssl 检查此链接
  • 感谢@walkingRed,虽然我仍然遇到来自 rightmoves api 的身份验证问题,所以证书一定有问题。
【解决方案3】:

我在 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);

【讨论】:

    猜你喜欢
    • 2010-10-18
    • 2011-10-31
    • 2018-09-29
    • 2012-09-17
    • 2021-02-20
    • 2022-01-26
    • 1970-01-01
    • 2020-12-09
    • 1970-01-01
    相关资源
    最近更新 更多