douou6696 2014-11-27 22:19
浏览 23
已采纳

PHP Curl未连接到HTTPS

I've been trying to connect to HTTPS servers using PHP and Curl. 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. I can run curl from shell and get to https sites. 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

  • 写回答

1条回答 默认 最新

  • doulang6695 2014-11-28 00:33
    关注

    You should try checking the error messages in curl_error(). See http://www.php.net/curl_error

    Furthermore Curl might have an outdated file to authenticate https certificates from. There's a new one on http://curl.haxx.se/ca/cacert.pem

    And use:

    curl_setopt($ch, CURLOPT_SSLVERSION, 3);
    

    to make sure the latest SSL version is used.

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?