-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseClient.php
More file actions
171 lines (151 loc) · 4.32 KB
/
Copy pathBaseClient.php
File metadata and controls
171 lines (151 loc) · 4.32 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/**
* @link http://www.tintsoft.com/
* @copyright Copyright (c) 2012 TintSoft Technology Co. Ltd.
* @license http://www.tintsoft.com/license/
*/
namespace xutl\aliyun;
use yii\di\Instance;
use yii\httpclient\Client;
use yii\base\InvalidConfigException;
use yii\httpclient\RequestEvent;
/**
* Class BaseClient
* @package xutl\aliyun
*/
abstract class BaseClient extends Client
{
const SIGNATURE_METHOD_HMACSHA1 = 'HMAC-SHA1';
const SIGNATURE_METHOD_HMACSHA256 = 'HMAC-SHA256';
/**
* @var string 阿里云AccessKey ID
*/
public $accessId;
/**
* @var string AccessKey
*/
public $accessKey;
/**
* @var string
*/
public $version;
/**
* @var string 区域ID
*/
public $regionId;
/**
* @var string
*/
public $securityToken;
/**
* @var string
*/
protected $signatureMethod = self::SIGNATURE_METHOD_HMACSHA1;
/**
* @var string
*/
protected $signatureVersion = '1.0';
/**
* @var string
*/
protected $dateTimeFormat = 'Y-m-d\TH:i:s\Z';
/**
* @var string|Aliyun
*/
private $aliyun = 'aliyun';
/**
* @inheritdoc
* @throws InvalidConfigException
*/
public function init()
{
parent::init();
if (empty ($this->version)) {
throw new InvalidConfigException ('The "version" property must be set.');
}
$this->aliyun = Instance::ensure($this->aliyun, Aliyun::class);
if (empty ($this->accessId)) {
$this->accessId = $this->aliyun->accessId;
}
if (empty ($this->accessKey)) {
$this->accessKey = $this->aliyun->accessKey;
}
$this->responseConfig['format'] = Client::FORMAT_JSON;
$this->on(Client::EVENT_BEFORE_SEND, [$this, 'RequestEvent']);
}
/**
* 请求事件
* @param RequestEvent $event
* @return void
*/
public function RequestEvent(RequestEvent $event)
{
$params = $event->request->getData();
$params['Version'] = $this->version;
$params['Format'] = 'JSON';
$params['AccessKeyId'] = $this->accessId;
$params['SignatureMethod'] = $this->signatureMethod;
$params['Timestamp'] = gmdate($this->dateTimeFormat);
$params['SignatureVersion'] = $this->signatureVersion;
$params['SignatureNonce'] = uniqid();
if ($this->regionId) {
$params['RegionId'] = $this->regionId;
}
if ($this->securityToken) {
$params['SecurityToken'] = $this->securityToken;
}
//参数排序
ksort($params);
$query = http_build_query($params, null, '&', PHP_QUERY_RFC3986);
$source = strtoupper($event->request->getMethod()) . '&%2F&' . $this->percentEncode($query);
//签名
if ($this->signatureMethod == self::SIGNATURE_METHOD_HMACSHA256) {
$params['Signature'] = base64_encode(hash_hmac('sha256', $source, $this->accessKey . '&', true));
} elseif ($this->signatureMethod == self::SIGNATURE_METHOD_HMACSHA1) {
$params['Signature'] = base64_encode(hash_hmac('sha1', $source, $this->accessKey . '&', true));
}
$event->request->setData($params);
}
/**
* 参数转码
* @param string $str
* @return mixed|string
*/
protected function percentEncode($str)
{
$res = urlencode($str);
$res = preg_replace('/\+/', '%20', $res);
$res = preg_replace('/\*/', '%2A', $res);
$res = preg_replace('/%7E/', '~', $res);
return $res;
}
/**
* 通过__call转发请求
* @param string $name 方法名
* @param array $arguments 参数
* @return array
*/
public function __call($name, $arguments)
{
$action = ucfirst($name);
$params = [];
if (is_array($arguments) && !empty($arguments)) {
$params = (array)$arguments[0];
}
$params['Action'] = $action;
return $this->_dispatchRequest($params);
}
/**
* 发起接口请求
* @param array $params 接口参数
* @return array
*/
protected function _dispatchRequest($params)
{
$response = $this->createRequest()
->setMethod('POST')
->setData($params)
->send();
return $response->data;
}
}