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

hs1rzwqc  于 2022-11-13  发布在  PHP
关注(0)|答案(2)|浏览(183)

我需要一些帮助来重写这个PHP curl代码,它在一个文件中使用*.pemCA cert),Client certprivate key

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);

因此,它可以在单独的文件中使用CA certificateClient CertificatePrivate Key
如以下命令行示例所示:
curl -d "var1=value1&var2=value2&..." -G -v --key key.pem --cacert ca.pem --cert client.pem:xxxxxx https://www.somesite.com/page

ttcibm8c

ttcibm8c1#

下面是一个PHP脚本,其中包含命令行调用的文字翻译:

<?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);
qyuhtwio

qyuhtwio2#

以下代码可用于发送带有密钥和证书的发布请求。
这段代码相当于下面的curl
curl --证书您的密钥。pem --密钥您的证书。密钥-d '授权类型=客户端证书&客户端ID= 1 &客户端密钥=2' https://accounts.youraccount.com/auth/oauth/v2/token

$CURLOPT_URL= "https://accounts.youraccount.com/auth/oauth/v2/token";
 $CURLOPT_POSTFIELDS= "grant_type=client_credential&client_id=1&client_secret=2";
    $clientCert = dirname(__FILE__) . '/certificates/yourkey.key';
    $clientKey = dirname(__FILE__) . "/certificates/yourcert.pem";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,  $CURLOPT_URL);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $CURLOPT_POSTFIELDS);
    curl_setopt($ch, CURLOPT_SSLKEY, $clientCert);
    curl_setopt($ch, CURLOPT_SSLCERT, $clientKey);
    $headers = array();
    $headers[] = 'Content-Type: application/x-www-form-urlencoded';
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_SSLCERT, $certFile);
    curl_setopt($ch, CURLOPT_SSLKEY, $clientKey);

    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    }
    print_r($result);
    curl_close($ch);
    exit;

相关问题