dtm37893 2012-07-03 09:42
浏览 129
已采纳

在php中使用curl,将客户端证书和私钥放在单独的文件中

I need some assistance rewriting this PHP curl code that uses *.pem (CA cert), Client cert and private key in one file:

curl_setopt($curl, CURLOPT_URL, $this->url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSLCERT, $this->keystore);
curl_setopt($curl, CURLOPT_CAINFO, $this->keystore);
curl_setopt($curl, CURLOPT_SSLKEYPASSWD, $this->keystorepassword);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

So it could use CA certificate, Client Certificate and Private Key in separate files.

As in this command-line example:

curl -d "var1=value1&var2=value2&..." -G -v --key key.pem --cacert ca.pem --cert client.pem:xxxxxx https://www.somesite.com/page

  • 写回答

1条回答 默认 最新

  • douyou7797 2012-07-03 11:49
    关注

    Here is a PHP script with a literal translation of your command line call:

    <?php
    
      $data = "var1=value1&var2=value2&...";
      $url = "https://www.somesite.com/page";
    
    
      $keyFile = "key.pem";
      $caFile = "ca.pem";
      $certFile = "client.pem";
      $certPass = "xxxxxx";
    
      // Initialise cURL
      $ch = curl_init($actualUrl);
    
      // The -d option is equivalent to CURLOPT_POSTFIELDS. But...
      // PHP's libcurl interface does not implement the -G flag - instead you would
      // append $data to $url like this:
      $actualUrl = $url.'?'.$data;
      curl_setopt($ch, CURLOPT_URL, $actualUrl);
    
      // The -v flag only makes sense at the command line, but it can be enabled
      // with CURLOPT_VERBOSE - in this case the information will be written to
      // STDERR, or the file specified by CURLOPT_STDERR. I will ignore this for
      // now, but if you would like a demonstration let me know.
    
      // The --key option - If your key file has a password, you will need to set
      // this with CURLOPT_SSLKEYPASSWD
      curl_setopt($ch, CURLOPT_SSLKEY, $keyFile);
    
      // The --cacert option
      curl_setopt($ch, CURLOPT_CAINFO, $caFile);
    
      // The --cert option
      curl_setopt($ch, CURLOPT_SSLCERT, $certFile);
      curl_setopt($ch, CURLOPT_SSLCERTPASSWD, $certPass);
    
      /*
        Now we should get an identical request to the one created by your command
        line string, let's have a look at some of the other options you set...
      */
    
      // CURLOPT_HEADER is disabled by default, there's no need for this unless you
      // enabled it earlier
      //curl_setopt($ch, CURLOPT_HEADER, 0);
    
      // Your command line string forces a GET request with the -G option, are you
      // trying to POST or GET?
      //curl_setopt($ch, CURLOPT_POST, true);
    
      // We don't need body data with a GET request
      //curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    
      // Since we've gone to all the trouble of supplying CS information, we might
      // as well validate it!
      //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿