php curl实现https双向认证的实例代码

发布时间:2020-06-08编辑:脚本学堂
分享一例php 借助curl实现https 双向认证的代码,例子比较简单,主要学习下https 双向认证在php代码下的实现方法,有需要的朋友参考下。

本节内容:
curl实现https双向认证

例子:
 

复制代码 代码示例:

<?php
/**
* https 双向认证
* edit: www.jb200.com
*/

$strurl="https://192.168.6.120/index.html"; 
function gethttpsdata($strurl){ 
    $tuCurl = curl_init();  
    curl_setopt($tuCurl, CURLOPT_URL, $strurl);  
    curl_setopt($tuCurl, CURLOPT_PORT , 443);  
    curl_setopt($tuCurl, CURLOPT_VERBOSE, 0);  
    curl_setopt($tuCurl, CURLOPT_HEADER, 0);  
    curl_setopt($tuCurl, CURLOPT_SSLVERSION, 3);  
     
    curl_setopt($tuCurl, CURLOPT_SSLCERT, getcwd()."/newcert.pem");  
    curl_setopt($tuCurl, CURLOPT_SSLCERTPASSWD,"xiaozl"); 
    curl_setopt($tuCurl, CURLOPT_SSLCERTTYPE,"PEM"); 
     
    curl_setopt($tuCurl, CURLOPT_SSLKEY, getcwd()."/newkey.pem");  
    curl_setopt($tuCurl, CURLOPT_SSLKEYPASSWD,"xiaozl"); 
    curl_setopt($tuCurl, CURLOPT_SSLKEYTYPE,"PEM");  
     
    curl_setopt($tuCurl, CURLOPT_CAINFO, getcwd()."/cacert.pem");  
      
    curl_setopt($tuCurl, CURLOPT_POST, 1);  
    curl_setopt($tuCurl, CURLOPT_SSL_VERIFYPEER, 1);  
    curl_setopt($tuCurl, CURLOPT_RETURNTRANSFER, 1);  
     
    //curl_setopt($tuCurl, CURLOPT_POSTFIELDS, $data);  
    //curl_setopt($tuCurl, CURLOPT_HTTPHEADER, array("Content-Type: text/xml","SOAPAction: "/soap/action/query"", "Content-length: ".strlen($data)));
     
    $tuData = curl_exec($tuCurl);  
    if(!curl_errno($tuCurl)){  
      $info = curl_getinfo($tuCurl);  
     // echo 'Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url'];  
    } else {  
      echo 'Curl error: ' . curl_error($tuCurl);  
    }  
    curl_close($tuCurl);  
    echo $tuData;