OpenCart添加第三方及自定义Library库

加入Library库

如添加网络库AppBlogHttp和RSA库AppBlogRsa

system/library/apollo/AppBlogHttp.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
class AppBlogHttp
{
public static function request_post($url = '', $params = '', array $headers = array())
{
if (empty($url) || empty($params)) {
return '{}';
}

$header = array(
"Accept: application/json",
"Content-Type: application/json",
);

if (!empty($headers)) {
foreach ($headers as $key => $val) {
$header[] = $key . ': ' . $val;
}
}

$jsonData = json_encode($params);
$postUrl = $url;
$ch = curl_init(); //初始化curl
curl_setopt($ch, CURLOPT_URL, $postUrl);
//curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
$data = curl_exec($ch); //运行curl
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$response = array();

$response['httpCode'] = $httpCode;
//$response['data'] = json_decode($data);
$response['data'] = $data;

//return json_encode($response, JSON_UNESCAPED_SLASHES);
return $response;
}

public static function request_get($url = '', $params = '')
{
if (empty($url) || empty($params)) {
return '{}';
}

$header = array(
"Accept: application/json",
//"Content-Type: application/json",
);

$urlData = createLinkstring($params);
$getUrl = $url . '?' . $urlData;

$ch = curl_init(); //初始化curl
curl_setopt($ch, CURLOPT_URL, $getUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch); //运行curl
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$response = array();

$response['httpCode'] = $httpCode;
//$response['data'] = json_decode($data);
$response['data'] = $data;

//return json_encode($response, JSON_UNESCAPED_SLASHES);
return $response;
}
}

system/library/appblog/AppBlogRsa.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class AppBlogRsa
{
/**
* RSA签名
* $data签名数据(需要先排序,然后拼接)
* 签名私钥,必须是没有经过pkcs8转换的私钥
* 最后的签名,需要用base64编码
* return Sign签名
*/
public static function Rsasign($data, $priKey, $sign_type=OPENSSL_ALGO_SHA1)
{
//$priKey = file_get_contents('../key/rsa_private_key.pem');
//转换为openssl密钥,必须是没有经过pkcs8转换的私钥
$res = openssl_get_privatekey($priKey);

//调用openssl内置签名方法,生成签名$sign
//openssl_sign($data, $sign, $res, OPENSSL_ALGO_MD5);
//openssl_sign($data, $sign, $res, OPENSSL_ALGO_SHA1);
openssl_sign($data, $sign, $res, $sign_type);

//释放资源
openssl_free_key($res);

//base64编码
$sign = base64_encode($sign);

return $sign;
}

/**RSA验签
* $data待签名数据(需要先排序,然后拼接)
* $sign需要验签的签名,需要base64_decode解码
* 验签公钥
* return 验签是否通过 bool值
*/
public static function Rsaverify($data, $sign, $pubKey, $sign_type=OPENSSL_ALGO_SHA1)
{
//读取连连支付公钥文件
//$pubKey = file_get_contents('../key/rsa_public_key.pem');

//转换为openssl格式密钥
$res = openssl_get_publickey($pubKey);

//调用openssl内置方法验签,返回bool值
$result = (bool) openssl_verify($data, base64_decode($sign), $res, $sign_type);

//释放资源
openssl_free_key($res);

//返回资源是否成功
return $result;
}
}

引用Library库

system/startup.php

1
2
3
require_once(DIR_SYSTEM . 'library/appblog/AppBlogCore.php');
require_once(DIR_SYSTEM . 'library/appblog/AppBlogRsa.php');
require_once(DIR_SYSTEM . 'library/appblog/AppBlogHttp.php');

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :