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;

}

相关推荐
Remember_993几秒前
Spring 中 REST API 调用工具对比:RestTemplate vs OpenFeign
java·网络·后端·算法·spring·php
Y_03几秒前
浅谈Java虚拟机JVM
java·开发语言·jvm
我命由我123451 分钟前
JUnit - 自定义 Rule
android·java·开发语言·数据库·junit·java-ee·android-studio
电商API&Tina3 分钟前
【电商API】淘宝/天猫拍立淘(按图搜索商品)API 全解析
大数据·开发语言·数据库·人工智能·json·图搜索算法
XerCis4 分钟前
Python读取硬盘信息pySMART——调用smartctl
开发语言·python·硬件架构
多多*8 分钟前
程序设计工作室1月28日内部训练赛 题解
java·开发语言·windows·哈希算法·散列表
2501_915921439 分钟前
在没有源码的前提下,怎么对 Swift 做混淆,IPA 混淆
android·开发语言·ios·小程序·uni-app·iphone·swift
weixin_4465042210 分钟前
Akshare:一个实用的免费金融数据Python库
开发语言·python·金融
IT陈图图3 小时前
构建 Flutter × OpenHarmony 跨端带文本输入对话框示例
开发语言·javascript·flutter
叫我辉哥e18 小时前
### 技术文章大纲:C语言造轮子大赛
c语言·开发语言