Using curl in php with client certificate and private key in separate files(在 php 中使用 curl 与单独文件中的客户端证书和私钥)
问题描述
我需要一些帮助来重写这个使用 *.pem
(CA cert
)、Client cert
和 private 的 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);
所以它可以在单独的文件中使用CA证书
、Client Certificate
和Private Key
.
So it could use CA certificate
, Client Certificate
and Private Key
in separate files.
在这个命令行示例中:
curl -d "var1=value1&var2=value2&..." -G -v --key key.pem --cacert ca.pem --cert client.pem:xxxxxx https://www.somesite.com/page
推荐答案
这是一个 PHP 脚本,其中包含您的命令行调用的文字翻译:
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);
这篇关于在 php 中使用 curl 与单独文件中的客户端证书和私钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 php 中使用 curl 与单独文件中的客户端证书和私钥
基础教程推荐
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01