【问题标题】:"SSLRead() return error -9806" when I try to use cURL on https当我尝试在 https 上使用 cURL 时出现“SSLRead() 返回错误 -9806”
【发布时间】:2017-08-15 07:53:12
【问题描述】:

我正在尝试通过 cURL 获取站点的内容,它在终端上工作得很好,但是它不适用于 PHP,下面是我的代码。

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://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",
    "referer: https://www.example.com/something",
    "user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:52.0) Gecko/20100101 Firefox/52.0"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

我已经在this question 上尝试过答案,但它对我不起作用,我得到了同样的错误..

我也尝试在我的代码中包含以下设置,它也不起作用。

  CURLOPT_SSL_VERIFYHOST => false,
  CURLOPT_SSL_VERIFYPEER => false,

我使用的是 Nginx 和 Laravel,我的 PHP 版本是 7.1.2,我的 cURL 版本是 7.51.0,我使用的是 macOS Sierra 10.12.3.

我怎样才能让它工作?

【问题讨论】:

  • 你试过$response = file_get_contents("https://example.com")
  • 在这种情况下我想使用 curl,因为在获取内容后我必须进行 POST 请求。
  • Error -9806 是 Apple SecureTransport 错误,而不是 OpenSSL 错误。 cURL 是针对 Apple 库而非 OpenSSL 库构建的。

标签: php macos curl nginx secure-transport


【解决方案1】:

重新安装带有 OpenSSL 支持的 curl。如果您使用的是Homebrew

brew remove curl
brew install openssl
brew install --with-openssl curl

【讨论】:

  • 这会产生Error: invalid option: --with-openssl
  • Homebrew 团队最近删除了 cURL 公式的所有安装选项,这意味着您现在将无法执行 brew install curl --with-openssl。相反,请执行brew install curl-openssl。请务必先卸载带有brew uninstall curl 的旧版本。
猜你喜欢
  • 2015-05-09
  • 2017-01-18
  • 1970-01-01
  • 2014-12-15
  • 1970-01-01
  • 2019-10-29
  • 1970-01-01
  • 2014-01-03
  • 2011-06-02
相关资源
最近更新 更多