Blame view

vendor/alibabacloud/client/src/Request/RpcRequest.php 5.76 KB
f0d6cde5   xu   vendor
1
2
3
4
<?php

namespace AlibabaCloud\Client\Request;

0c34aba8   xu   更新阿里云短信接口
5
6
7
8
use AlibabaCloud\Client\Credentials\AccessKeyCredential;
use AlibabaCloud\Client\Credentials\BearerTokenCredential;
use AlibabaCloud\Client\Credentials\CredentialsInterface;
use AlibabaCloud\Client\Credentials\StsCredential;
f0d6cde5   xu   vendor
9
10
use AlibabaCloud\Client\Exception\ClientException;
use RuntimeException;
0c34aba8   xu   更新阿里云短信接口
11
12

/**
f0d6cde5   xu   vendor
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 * RESTful RPC Request.
 *
 * @package   AlibabaCloud\Client\Request
 */
class RpcRequest extends Request
{

    /**
     * @var string
     */
    private $dateTimeFormat = 'Y-m-d\TH:i:s\Z';

    /**
     * Resolve request parameter.
     *
     * @param AccessKeyCredential|BearerTokenCredential|StsCredential $credential
     *
f0d6cde5   xu   vendor
30
31
     * @throws ClientException
     */
0c34aba8   xu   更新阿里云短信接口
32
    public function resolveParameters($credential)
f0d6cde5   xu   vendor
33
    {
0c34aba8   xu   更新阿里云短信接口
34
35
36
        $this->resolveQuery($credential);

        $this->options['query']['Signature'] = $this->signature(
f0d6cde5   xu   vendor
37
38
39
            $this->options['query'],
            $credential->getAccessKeySecret()
        );
0c34aba8   xu   更新阿里云短信接口
40

f0d6cde5   xu   vendor
41
        if ($this->method === 'POST') {
0c34aba8   xu   更新阿里云短信接口
42
            foreach ($this->options['query'] as $apiParamKey => $apiParamValue) {
f0d6cde5   xu   vendor
43
44
                $this->options['form_params'][$apiParamKey] = $apiParamValue;
            }
0c34aba8   xu   更新阿里云短信接口
45
46
47
48
49
50
            unset($this->options['query']);
        }
    }

    /**
     * Resolve request query.
f0d6cde5   xu   vendor
51
     *
f0d6cde5   xu   vendor
52
53
54
55
56
57
58
59
60
     * @param AccessKeyCredential|BearerTokenCredential|StsCredential $credential
     *
     * @throws ClientException
     */
    private function resolveQuery($credential)
    {
        if (isset($this->options['query'])) {
            foreach ($this->options['query'] as $key => $value) {
                $this->options['query'][$key] = self::booleanValueToString($value);
0c34aba8   xu   更新阿里云短信接口
61
            }
f0d6cde5   xu   vendor
62
63
        }
        $signature = $this->httpClient()->getSignature();
0c34aba8   xu   更新阿里云短信接口
64
        if ($credential->getAccessKeyId()) {
f0d6cde5   xu   vendor
65
66
67
68
69
70
            $this->options['query']['AccessKeyId'] = $credential->getAccessKeyId();
        }
        $this->options['query']['RegionId']         = $this->realRegionId();
        $this->options['query']['Format']           = $this->format;
        $this->options['query']['SignatureMethod']  = $signature->getMethod();
        $this->options['query']['SignatureVersion'] = $signature->getVersion();
0c34aba8   xu   更新阿里云短信接口
71
72
73
74
        if ($signature->getType()) {
            $this->options['query']['SignatureType'] = $signature->getType();
        }
        $this->options['query']['SignatureNonce'] = md5(uniqid(mt_rand(), true));
f0d6cde5   xu   vendor
75
        $this->options['query']['Timestamp']      = gmdate($this->dateTimeFormat);
0c34aba8   xu   更新阿里云短信接口
76
        $this->options['query']['Action']         = $this->action;
f0d6cde5   xu   vendor
77
        $this->options['query']['Version']        = $this->version;
0c34aba8   xu   更新阿里云短信接口
78
79
80
81
82
83
84
85
86
87
        $this->resolveSecurityToken($credential);
        $this->resolveBearerToken($credential);
    }

    /**
     * Convert a Boolean value to a string.
     *
     * @param bool|string $value
     *
     * @return string
f0d6cde5   xu   vendor
88
     */
0c34aba8   xu   更新阿里云短信接口
89
90
91
92
93
94
95
96
97
    private static function booleanValueToString($value)
    {
        if (is_bool($value)) {
            if ($value) {
                return 'true';
            }

            return 'false';
        }
f0d6cde5   xu   vendor
98
99
100

        return $value;
    }
0c34aba8   xu   更新阿里云短信接口
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119

    /**
     * @param CredentialsInterface $credential
     */
    private function resolveSecurityToken(CredentialsInterface $credential)
    {
        if ($credential instanceof StsCredential && $credential->getSecurityToken()) {
            $this->options['query']['SecurityToken'] = $credential->getSecurityToken();
        }
    }

    /**
     * @param CredentialsInterface $credential
     */
    private function resolveBearerToken(CredentialsInterface $credential)
    {
        if ($credential instanceof BearerTokenCredential) {
            $this->options['query']['BearerToken'] = $credential->getBearerToken();
        }
f0d6cde5   xu   vendor
120
    }
0c34aba8   xu   更新阿里云短信接口
121

f0d6cde5   xu   vendor
122
    /**
0c34aba8   xu   更新阿里云短信接口
123
124
     * Sign the parameters.
     *
f0d6cde5   xu   vendor
125
126
127
128
129
130
     * @param array  $parameters
     * @param string $accessKeySecret
     *
     * @return mixed
     * @throws ClientException
     */
f0d6cde5   xu   vendor
131
132
    private function signature($parameters, $accessKeySecret)
    {
0c34aba8   xu   更新阿里云短信接口
133
        ksort($parameters);
f0d6cde5   xu   vendor
134
        $canonicalizedQuery = '';
0c34aba8   xu   更新阿里云短信接口
135
        foreach ($parameters as $key => $value) {
f0d6cde5   xu   vendor
136
            $canonicalizedQuery .= '&' . $this->percentEncode($key) . '=' . $this->percentEncode($value);
f0d6cde5   xu   vendor
137
138
        }

0c34aba8   xu   更新阿里云短信接口
139
140
141
142
        $this->stringToBeSigned = $this->method
                                  . '&%2F&'
                                  . $this->percentEncode(substr($canonicalizedQuery, 1));

f0d6cde5   xu   vendor
143
144
145
        return $this->httpClient()
                    ->getSignature()
                    ->sign($this->stringToBeSigned, $accessKeySecret . '&');
0c34aba8   xu   更新阿里云短信接口
146
    }
f0d6cde5   xu   vendor
147

0c34aba8   xu   更新阿里云短信接口
148
    /**
f0d6cde5   xu   vendor
149
     * @param string $string
0c34aba8   xu   更新阿里云短信接口
150
151
152
153
154
155
     *
     * @return null|string|string[]
     */
    protected function percentEncode($string)
    {
        $result = urlencode($string);
f0d6cde5   xu   vendor
156
        $result = str_replace(['+', '*'], ['%20', '%2A'], $result);
0c34aba8   xu   更新阿里云短信接口
157
158
159
160
161
162
163
164
165
166
167
        $result = preg_replace('/%7E/', '~', $result);

        return $result;
    }

    /**
     * Magic method for set or get request parameters.
     *
     * @param string $name
     * @param mixed  $arguments
     *
f0d6cde5   xu   vendor
168
169
170
171
172
173
174
175
176
177
178
179
     * @return $this
     */
    public function __call($name, $arguments)
    {
        if (\strpos($name, 'get') === 0) {
            $parameterName = $this->propertyNameByMethodName($name);

            return $this->__get($parameterName);
        }

        if (\strpos($name, 'with') === 0) {
            $parameterName = $this->propertyNameByMethodName($name, 4);
0c34aba8   xu   更新阿里云短信接口
180
181
            $this->__set($parameterName, $arguments[0]);
            $this->options['query'][$parameterName] = $arguments[0];
f0d6cde5   xu   vendor
182

0c34aba8   xu   更新阿里云短信接口
183
            return $this;
f0d6cde5   xu   vendor
184
185
        }

0c34aba8   xu   更新阿里云短信接口
186
187
188
189
        if (\strpos($name, 'set') === 0) {
            $parameterName = $this->propertyNameByMethodName($name);
            $withMethod    = "with$parameterName";

f0d6cde5   xu   vendor
190
191
192
193
            return $this->$withMethod($arguments[0]);
        }

        throw new RuntimeException('Call to undefined method ' . __CLASS__ . '::' . $name . '()');
0c34aba8   xu   更新阿里云短信接口
194
195
    }
}
f0d6cde5   xu   vendor

0c34aba8   xu   更新阿里云短信接口

f0d6cde5   xu   vendor