【问题标题】:PHP Curl Not connecting to HTTPSPHP Curl 未连接到 HTTPS
【发布时间】:2014-11-27 22:19:00
【问题描述】:

我一直在尝试使用 PHP 和 Curl 连接到 HTTPS 服务器。我没有收到任何错误或响应。

$options = array(
  CURLOPT_RETURNTRANSFER => true,     // return web page
  CURLOPT_HEADER         => true,    //  return headers
  CURLOPT_FOLLOWLOCATION => true,     // follow redirects
  CURLOPT_ENCODING       => "",       // handle all encodings
  CURLOPT_USERAGENT      => "spider", // who am i
  CURLOPT_AUTOREFERER    => true,     // set referer on redirect
  CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
  CURLOPT_TIMEOUT        => 120,      // timeout on response
  CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
  CURLOPT_SSL_VERIFYPEER => true,
  CURLOPT_SSL_VERIFYHOST => 2,
  CURLOPT_CAINFO         => 'ca.pem'
);

$ch = curl_init("https://www.google.com");
curl_setopt_array( $ch, $options );

var_dump(curl_exec($ch));
var_dump(curl_getinfo($ch));
var_dump(curl_errno($ch));
var_dump(curl_error($ch));

输出是:

bool(false)
array(22) {
  ["url"]=>
  string(23) "https://www.google.com/"
  ["content_type"]=>
  NULL
  ["http_code"]=>
  int(0)
  ["header_size"]=>
  int(0)
  ["request_size"]=>
  int(0)
  ["filetime"]=>
  int(-1)
  ["ssl_verify_result"]=>
  int(0)
  ["redirect_count"]=>
  int(0)
  ["total_time"]=>
  float(0.004288)
  ["namelookup_time"]=>
  float(0.004212)
  ["connect_time"]=>
  float(0.006157)
  ["pretransfer_time"]=>
  float(0)
  ["size_upload"]=>
  float(0)
  ["size_download"]=>
  float(0)
  ["speed_download"]=>
  float(0)
  ["speed_upload"]=>
  float(0)
  ["download_content_length"]=>
  float(-1)
  ["upload_content_length"]=>
  float(-1)
  ["starttransfer_time"]=>
  float(0)
  ["redirect_time"]=>
  float(0)
  ["certinfo"]=>
  array(0) {
  }
  ["redirect_url"]=>
  string(0) ""
}
int(77)
string(0) ""

如果我将 url 更改为 http://www.google.com,脚本会按预期返回 google 页面。我可以从 shell 运行 curl 并访问 https 站点。我不确定还可以尝试什么,因为没有出现错误或其他输出。

Curl 版本是 7.38.0 PHP版本是5.3.29

【问题讨论】:

    标签: php ssl curl https


    【解决方案1】:

    您应该尝试检查curl_error() 中的错误消息。 见http://www.php.net/curl_error

    此外,Curl 可能有一个过时的文件来验证 https 证书。 http://curl.haxx.se/ca/cacert.pem有一个新的

    并使用:

    curl_setopt($ch, CURLOPT_SSLVERSION, 3);
    

    确保使用最新的 SSL 版本。

    这些设置:

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    

    也可以。

    但这意味着安全风险。不要在“生产”服务器上执行此操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-17
      相关资源
      最近更新 更多