duanfu3634 2017-08-02 04:54
浏览 134

PHP - 使用HTTPS的CURL [关闭]

I'm trying to get Json Data from a https url.

I have tested and working code to do just this for http... but I am yet to find a working version for https. This needs to be secure code, not quick fixes please...

This is my http version that works

<?php
$key = "admin"; //user
$secret = "admin"; //pass
$api_ep = "http://$Hostname:$Port/$address";


if ($key && $secret){
$curl_opts = array(
  CURLOPT_HTTPHEADER=>array("Accept: application/json"),
  CURLOPT_VERBOSE=>false,
  CURLOPT_HEADER=>false,
  CURLOPT_POST=>false,
  CURLOPT_RETURNTRANSFER=>true,
  CURLOPT_HTTPAUTH=>CURLAUTH_DIGEST,
  CURLOPT_USERPWD=>$key.":".$secret,
  CURLOPT_URL=> $api_ep  
);
}

function disp($opts,$var){
  $ch = curl_init();

  curl_setopt_array($ch, $opts);
  $raw_resp = curl_exec($ch);
//  $array_resp = json_decode($raw_resp);

  //print_r($array_resp);
  print_r($raw_resp);
  curl_close($ch);
//$array = json_decode($raw_resp, true);
//print_r($array_resp);
//disp_table($array_resp, $var);
}

disp($curl_opts,$Type);
?>

Thanks in advanced!

  • 写回答

3条回答 默认 最新

  • douqiao5314 2017-08-02 05:03
    关注

    Add the following to disable SSL verification.

    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    

    Curl SSL fails to verify self signed certificates and hence, you will not be able to connect with https hosts if they are using self signed certificates.

    function disp($opts,$var){
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
      curl_setopt_array($ch, $opts);
      $raw_resp = curl_exec($ch);
      print_r($raw_resp);
      curl_close($ch);
    }
    
    评论

报告相同问题?