简体   繁体   English

PHP Curl无法连接到HTTPS

[英]PHP Curl Not connecting to HTTPS

I've been trying to connect to HTTPS servers using PHP and Curl. 我一直在尝试使用PHP和Curl连接到HTTPS服务器。 I don't get any error or response. 我没有收到任何错误或回应。

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

The Output is: 输出为:

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) ""

If I change the url to http://www.google.com , the script returns the google page as expected. 如果我将网址更改为http://www.google.com ,脚本将按预期返回Google页面。 I can run curl from shell and get to https sites. 我可以从shell运行curl并访问https站点。 I'm not sure what else to try since there are no errors or other output presented. 我不确定还有什么尝试,因为没有出现错误或其他输出。

Curl version is 7.38.0 PHP version is 5.3.29 Curl版本是7.38.0 PHP版本是5.3.29

You should try checking the error messages in curl_error() . 您应该尝试检查curl_error()的错误消息。 See http://www.php.net/curl_error 参见http://www.php.net/curl_error

Furthermore Curl might have an outdated file to authenticate https certificates from. 此外,Curl可能具有过时的文件来从中验证https证书。 There's a new one on http://curl.haxx.se/ca/cacert.pem http://curl.haxx.se/ca/cacert.pem上有一个新的

And use: 并使用:

curl_setopt($ch, CURLOPT_SSLVERSION, 3);

to make sure the latest SSL version is used. 确保使用最新的SSL版本。

These settings: 这些设置:

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

also work. 也可以。

But this means a security risk. 但这意味着安全隐患。 Don't do it on 'production' servers. 不要在“生产”服务器上这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM