PHP的SHA256WithRSA签名和Curl POST请求函数
<?php
// 引入openssl扩展
extension=php_openssl.dll
// 定义一个函数用于生成SHA256WithRSA签名
function generateRSA256Signature($data, $privateKey) {
$signature = "";
openssl_sign($data, $signature, $privateKey, 'SHA256');
return base64_encode($signature);
}
// 定义一个函数用于发送Curl POST请求
function sendCurlPostRequest($url, $data, $headers = array()) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
// 使用示例
$url = "https://your-api-endpoint.com";
$data = array("key1" => "value1", "key2" => "value2");
$privateKey = "-----BEGIN PRIVATE KEY-----
...
-----END PRIVATE KEY-----";
// 生成签名
$signature = generateRSA256Signature(json_encode($data), $privateKey);
$headers = array("Authorization: Bearer " . $signature);
// 发送POST请求
$response = sendCurlPostRequest($url, $data, $headers);
echo $response;
?>
这个代码示例提供了两个函数:generateRSA256Signature
用于生成SHA256WithRSA签名,sendCurlPostRequest
用于发送Curl POST请求。使用时需要替换$url
, $data
, $privateKey
为实际值,并根据API的要求设置请求头部$headers
。
评论已关闭