php - cURL 失败并出现错误 : Couldn't understand the server certificate format

标签 php macos apache curl

Mac OSX El Capitan,默认 apache 安装在本地主机上,brew 安装 php70。下面的代码使用 cli (php -f test.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 版本"

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

解决方案

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

    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 already provides this software and installing another version in parallel can cause all kinds of trouble.

我还没有足够长的时间来验证任何存在/不存在的问题。


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

关于php - cURL 失败并出现错误 : Couldn't understand the server certificate format,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38707493/

相关文章:

java - 如何在 mac osx 上加载 libgdx 桌面应用程序?

swift - 将 Common Crypto AESEncryption 迁移到 CryptoSWIFT,因为在某些情况下解密字符串变为空

php - 如何从主文件夹使用 Apache 运行 PHP 项目

php - HTML/CSS : Image Not being displayed

php - 显示数据库中的一行并移至下一列

javascript - keydown 上的 jQuery 下拉 ajax 搜索是 'one step behind'

php - MYSQL - 如何使用 ORDER BY 和 LIMIT 对列进行排序,然后按另一列对这些结果进行排序

java - Eclipse 未针对 MacOS Sierra 启动

java - 微服务架构 |无法分配请求的地址

image - 存储图像以在 SOLR 搜索结果中显示