cURL失败,错误为:Cand';无法理解服务器证书格式


cURL fails with error: Couldn't understand the server certificate format

Mac OSX El Capitan,默认apache安装在localhost上,brew安装php70。以下代码使用cli(php-ftest.php)工作,但当从apache运行时,我会得到以下内容。

SSL certificate problem: Couldn't understand the server certificate format

使用"http"URL在这两种情况下都很好。同样的设置在Ubuntu机器上运行良好。我在干净安装El Capitan之前就做过这项工作,我模糊地记得一些关于Mac OSX和openssl for curl的东西,但在这里找不到区别。

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.example.com/",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "cache-control: no-cache"
  ),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

我遇到了同样的问题,经过大量搜索后找到了解决方案。。。

我正在使用php56,但我看不出为什么这不适用于php70

验证相同问题

首先检查PHP cURL库是否使用Mac OS(X)内置版本的SSL:
php -i | grep "SSL Version"

如果你得到了SSL Version => SecureTransport,那么它使用的是Mac OS(X)内置版本,这似乎是问题的核心。

解决方案

  1. 为了解决这个问题,您必须安装cURL库的Homebrew版本:

    brew install curl --with-libssh2 --with-openssl
    
  2. 然后使用以下两个选项重新安装PHP:
    --with-homebrew-curl --with-homebrew-openssl
    (包括您需要的任何选项)

    brew install php56 --with-homebrew-curl --with-homebrew-openssl (--with-apache ...)
    

    或者对于PHP 7.2:

    brew reinstall php72 --with-apache --with-homebrew-curl --with-homebrew-libxslt --with-homebrew-openssl --without-snmp
    

验证

php -i | grep "SSL Version"

应给予:

SSL Version => OpenSSL/1.0.2j

注意:安装Homebrew cURL时,他们会发出警告:

macOS已经提供了该软件,并在中安装了另一个版本并行可能会引起各种各样的麻烦。

我在这种情况下生活的时间不够长,无法核实任何当前/缺席的问题。


来源:https://www.farces.com/wikis/naked-server/php/php-openssl/

找不到关于此错误的太多文档,该错误源于Darwin SSL错误9808 errSSLBadCert

如果使用OpenSSL而不是Darwin SSL编译cURL,您可能会更幸运。

关于这个错误的其他一些互联网搜索表明它可能在读取你的钥匙链时遇到问题,但我不确定。