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.

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

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c