1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlibabaCloud\Client\Request; |
4
|
|
|
|
5
|
|
|
use AlibabaCloud\Client\Credentials\AccessKeyCredential; |
6
|
|
|
use AlibabaCloud\Client\Credentials\BearerTokenCredential; |
7
|
|
|
use AlibabaCloud\Client\Credentials\CredentialsInterface; |
8
|
|
|
use AlibabaCloud\Client\Credentials\StsCredential; |
9
|
|
|
use AlibabaCloud\Client\Exception\ClientException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* RESTful RPC Request. |
13
|
|
|
* |
14
|
|
|
* @package AlibabaCloud\Client\Request |
15
|
|
|
*/ |
16
|
|
|
class RpcRequest extends Request |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $dateTimeFormat = 'Y-m-d\TH:i:s\Z'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Resolve request parameter. |
26
|
|
|
* |
27
|
|
|
* @param AccessKeyCredential|BearerTokenCredential|StsCredential $credential |
28
|
|
|
* |
29
|
|
|
* @throws ClientException |
30
|
63 |
|
*/ |
31
|
|
|
public function resolveParameters($credential) |
32
|
63 |
|
{ |
33
|
35 |
|
$this->resolveQuery($credential); |
34
|
35 |
|
|
35
|
35 |
|
$this->options['query']['Signature'] = $this->signature( |
36
|
35 |
|
$this->options['query'], |
37
|
63 |
|
$credential->getAccessKeySecret() |
38
|
63 |
|
); |
39
|
63 |
|
|
40
|
63 |
|
if ($this->method === 'POST') { |
41
|
63 |
|
foreach ($this->options['query'] as $apiParamKey => $apiParamValue) { |
42
|
63 |
|
$this->options['form_params'][$apiParamKey] = $apiParamValue; |
43
|
63 |
|
} |
44
|
22 |
|
unset($this->options['query']); |
45
|
22 |
|
} |
46
|
63 |
|
} |
47
|
63 |
|
|
48
|
63 |
|
/** |
49
|
63 |
|
* Resolve request query. |
50
|
63 |
|
* |
51
|
2 |
|
* @param AccessKeyCredential|BearerTokenCredential|StsCredential $credential |
52
|
2 |
|
* |
53
|
63 |
|
* @throws ClientException |
54
|
9 |
|
*/ |
55
|
9 |
|
private function resolveQuery($credential) |
56
|
63 |
|
{ |
57
|
|
|
if (isset($this->options['query'])) { |
58
|
|
|
foreach ($this->options['query'] as $key => $value) { |
59
|
|
|
$this->options['query'][$key] = self::booleanValueToString($value); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
$signature = $this->httpClient()->getSignature(); |
63
|
|
|
$this->options['query']['RegionId'] = $this->realRegionId(); |
64
|
|
|
$this->options['query']['AccessKeyId'] = $credential->getAccessKeyId(); |
65
|
61 |
|
$this->options['query']['Format'] = $this->format; |
66
|
|
|
$this->options['query']['SignatureMethod'] = $signature->getMethod(); |
67
|
61 |
|
$this->options['query']['SignatureVersion'] = $signature->getVersion(); |
68
|
|
|
if ($signature->getType()) { |
69
|
61 |
|
$this->options['query']['SignatureType'] = $signature->getType(); |
70
|
61 |
|
} |
71
|
61 |
|
$this->options['query']['SignatureNonce'] = md5(uniqid(mt_rand(), true)); |
72
|
61 |
|
$this->options['query']['Timestamp'] = gmdate($this->dateTimeFormat); |
73
|
|
|
$this->options['query']['Action'] = $this->action; |
74
|
52 |
|
$this->options['query']['Version'] = $this->version; |
75
|
33 |
|
$this->resolveSecurityToken($credential); |
76
|
33 |
|
$this->resolveBearerToken($credential); |
77
|
33 |
|
} |
78
|
33 |
|
|
79
|
33 |
|
/** |
80
|
52 |
|
* Convert a Boolean value to a string. |
81
|
|
|
* |
82
|
|
|
* @param bool|string $value |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
private static function booleanValueToString($value) |
87
|
|
|
{ |
88
|
|
|
if (is_bool($value)) { |
89
|
42 |
|
if ($value) { |
90
|
|
|
return 'true'; |
91
|
42 |
|
} |
92
|
3 |
|
|
93
|
2 |
|
return 'false'; |
94
|
|
|
} |
95
|
|
|
|
96
|
2 |
|
return $value; |
97
|
|
|
} |
98
|
|
|
|
99
|
39 |
|
/** |
100
|
|
|
* @param CredentialsInterface $credential |
101
|
|
|
*/ |
102
|
|
|
private function resolveSecurityToken(CredentialsInterface $credential) |
103
|
|
|
{ |
104
|
|
|
if ($credential instanceof StsCredential && $credential->getSecurityToken()) { |
105
|
|
|
$this->options['query']['SecurityToken'] = $credential->getSecurityToken(); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param CredentialsInterface $credential |
111
|
63 |
|
*/ |
112
|
|
|
private function resolveBearerToken(CredentialsInterface $credential) |
113
|
63 |
|
{ |
114
|
63 |
|
if ($credential instanceof BearerTokenCredential) { |
115
|
63 |
|
$this->options['query']['BearerToken'] = $credential->getBearerToken(); |
116
|
63 |
|
} |
117
|
63 |
|
} |
118
|
|
|
|
119
|
63 |
|
/** |
120
|
|
|
* Sign the parameters. |
121
|
63 |
|
* |
122
|
|
|
* @param array $parameters |
123
|
63 |
|
* @param string $accessKeySecret |
124
|
63 |
|
* |
125
|
63 |
|
* @return mixed |
126
|
|
|
* @throws ClientException |
127
|
|
|
*/ |
128
|
|
|
private function signature($parameters, $accessKeySecret) |
129
|
|
|
{ |
130
|
|
|
ksort($parameters); |
131
|
|
|
$canonicalizedQuery = ''; |
132
|
|
|
foreach ($parameters as $key => $value) { |
133
|
66 |
|
$canonicalizedQuery .= '&' . $this->percentEncode($key) . '=' . $this->percentEncode($value); |
134
|
|
|
} |
135
|
66 |
|
|
136
|
66 |
|
$this->stringToBeSigned = $this->method |
137
|
66 |
|
. '&%2F&' |
138
|
|
|
. $this->percentEncode(substr($canonicalizedQuery, 1)); |
139
|
66 |
|
|
140
|
|
|
return $this->httpClient() |
141
|
|
|
->getSignature() |
142
|
|
|
->sign($this->stringToBeSigned, $accessKeySecret . '&'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param string $string |
147
|
|
|
* |
148
|
|
|
* @return null|string|string[] |
149
|
|
|
*/ |
150
|
2 |
|
protected function percentEncode($string) |
151
|
|
|
{ |
152
|
2 |
|
$result = urlencode($string); |
153
|
2 |
|
$result = str_replace(['+', '*'], ['%20', '%2A'], $result); |
154
|
|
|
$result = preg_replace('/%7E/', '~', $result); |
155
|
2 |
|
|
156
|
|
|
return $result; |
157
|
|
|
} |
158
|
2 |
|
|
159
|
2 |
|
/** |
160
|
2 |
|
* Magic method for set or get request parameters. |
161
|
2 |
|
* |
162
|
2 |
|
* @param string $name |
163
|
|
|
* @param mixed $arguments |
164
|
2 |
|
* |
165
|
|
|
* @return $this |
166
|
|
|
*/ |
167
|
|
|
public function __call($name, $arguments) |
168
|
|
|
{ |
169
|
|
|
if (\strpos($name, 'get') !== false) { |
170
|
|
|
$parameterName = $this->propertyNameByMethodName($name); |
171
|
|
|
|
172
|
|
|
return $this->__get($parameterName); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
if (\strpos($name, 'with') !== false) { |
176
|
|
|
$parameterName = $this->propertyNameByMethodName($name, 4); |
177
|
|
|
$this->__set($parameterName, $arguments[0]); |
178
|
|
|
$this->options['query'][$parameterName] = $arguments[0]; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $this; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|