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