PHP的SHA256WithRSA签名和Curl POST请求函数

getCustomerEncryptionKey - 获取加密秘钥

/**

* 获取加密秘钥

* @param array $params

* @return string

*/

public function getCustomerEncryptionKey(array $params): string

{

//1.ASCII码(字典序-升序)排序

ksort($params);

//2.拼接成字符串string

$string = '';

foreach ($params as $key => $v) {

string .= (string)(key . '=' . $v . '&');

}

//3.拼接得到signTemp字符串

$signTemp = string . 'MerchantKey=' . self::MerchantKey;

//4.获取RSA私钥信息

$absPath = dirname(FILE) . '/test_pri.txt';//(对接替换为自己的私钥)

content = file_get_contents(absPath);

sContent = openssl_pkey_get_private(content);

//5.PHP-SHA256WithRSA 签名算法加密

openssl_sign($signTemp, $signature, $sContent, "SHA256");

openssl_free_key($sContent);

//6.base64转换,得到SignSecret值

return base64_encode($signature);

}

getCurlRequestResult - Curl POST请求函数

/**

* Curl请求函数

* @param string $url 请求地址

* @param array $params 实体参数

* @param int $isPost GET|POST

* @param int $https HTTP|HTTPS

* @param int $isJson Json|-

* @return bool|string

*/

public function getCurlRequestResult($url, params,isPost = 0,https = 0,isJson = 0)

{

$httpInfo = array();

$ch = curl_init();

curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);

curl_setopt($ch, CURLOPT_USERAGENT,

'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36');

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);

curl_setopt($ch, CURLOPT_TIMEOUT, 30);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

if ($https) {

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在

}

if ($isPost) {

curl_setopt($ch, CURLOPT_POST, true);

curl_setopt($ch, CURLOPT_POSTFIELDS, $params);

curl_setopt($ch, CURLOPT_URL, $url);

} else {

if ($params) {

if (is_array($params)) {

params = http_build_query(params);

}

curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);

} else {

curl_setopt($ch, CURLOPT_URL, $url);

}

}

if ($isJson) {

curl_setopt($ch, CURLOPT_HEADER, 0);

curl_setopt($ch, CURLOPT_HTTPHEADER, array(

'Content-Type: application/x-www-form-urlencoded; charset=utf-8',

"Accept: application/x-www-form-urlencoded",

'Cache-Control: no-cache',

'Pragma: no-cache'

));

}

response = curl_exec(ch);

if ($response === false) {

return false;

}

httpCode = curl_getinfo(ch, CURLINFO_HTTP_CODE);

httpInfo = array_merge(httpInfo, curl_getinfo($ch));

curl_close($ch);

return $response;

}

相关推荐
DieSnowK3 分钟前
[C++][设计模式][备忘录模式]详细讲解
开发语言·c++·设计模式·重构·面向对象·备忘录模式·新手向
左手の明天17 分钟前
【Python网络爬虫案例】python爬虫之模拟登录
开发语言·爬虫·python·模拟登录
mumu_wangwei1 小时前
【PHP】实现类的无缝动态扩展,设计模式,php工厂模式应用场景,以下代码是工厂模式在框架设计中的真实使用案例代码
开发语言·设计模式·php
lsjweiyi1 小时前
sitemap.xml生成(go语言版)
开发语言·golang·sitemap.xml
CoCo玛奇朵1 小时前
CleanMyMacX2024免费且强大的mac电脑系统优化工具
开发语言·javascript·macos·ffmpeg·ecmascript·百度云
Itmastergo1 小时前
零基础小白学习 Python,应该如何配置 Python 开发环境?(包含Windows、MacOS、Linux)
开发语言·python·学习
Dongliner~1 小时前
【C++:list】
开发语言·c++
nbplus_0071 小时前
golang跨平台GUI框架fyne介绍与使用详解,开放案例
开发语言·后端·安全·golang·个人开发
情系明明1 小时前
赋值运算符函数 c++
开发语言·c++
点云侠1 小时前
SARscape——多视滤波
开发语言·人工智能·算法·计算机视觉