使用PHP CURL的SSL证书

8
我使用curl将一个xml文件通过https发送给rightmove API-他们向我提供了所有证书。
我遇到了以下错误:
60 SSL证书问题:无法获取本地签发人证书结果=
我尝试了在其他类似的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需要身份验证。 - Gaz Smith
你的浏览器似乎没有出现同样的错误。可能是因为你没有及时更新补丁,或者安装的 PHP 版本没有正确配置适用于你的平台。 - symcbean
我在本地、Web服务器以及工作服务器上都遇到了这个错误。@symcbean,但是您说得对,我可以在浏览器中轻松导航。 - Gaz Smith
经过一整天的尝试,我仍然无法解决这个问题。 - Gaz Smith
3个回答

9

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

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

1
请问您能否粘贴整个curl请求呢?我也在为Rightmove的Curl请求苦苦挣扎了几天。 - tousif
谢谢,这个帮助了我到目前为止。 - tousif

3

由于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(从其他帖子上找到的多个文件),但它并没有改变任何东西。 - Gaz Smith
当然,如果我尝试不验证主机,API会返回身份验证失败。 - Gaz Smith
当我更改curl.cainfo时,我得到了这个错误:60 SSL证书问题:无法获取本地颁发者证书结果 = - Gaz Smith
@GazSmith 请查看此链接,其中介绍了如何使用curl ssl(http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/)。 - Tomasz Ferfecki
谢谢@walkingRed,虽然我仍然在从rightmoves api中遇到身份验证问题,因此证书必须有错的地方。 - Gaz Smith

0

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接