1+ <?php
2+
3+ /*
4+ * Licensed to the Apache Software Foundation (ASF) under one
5+ * or more contributor license agreements. See the NOTICE file
6+ * distributed with this work for additional information
7+ * regarding copyright ownership. The ASF licenses this file
8+ * to you under the Apache License, Version 2.0 (the
9+ * "License"); you may not use this file except in compliance
10+ * with the License. You may obtain a copy of the License at
11+ *
12+ * http://www.apache.org/licenses/LICENSE-2.0
13+ *
14+ * Unless required by applicable law or agreed to in writing,
15+ * software distributed under the License is distributed on an
16+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+ * KIND, either express or implied. See the License for the
18+ * specific language governing permissions and limitations
19+ * under the License.
20+ */
21+
22+ namespace aliyun \dm ;
23+
24+ use aliyun \dm \auth \ShaHmac1Signer ;
25+
26+ class Client
27+ {
28+ /**
29+ * @var string
30+ */
31+ public $ accessKeyId ;
32+
33+ /**
34+ * @var string
35+ */
36+ public $ accessSecret ;
37+
38+ /**
39+ * @var \aliyun\core\auth\SignerInterface 签名算法实例
40+ */
41+ public $ signer ;
42+
43+ /**
44+ * @var \GuzzleHttp\Client
45+ */
46+ public $ _httpClient ;
47+
48+ /**
49+ * Request constructor.
50+ * @param array $config
51+ */
52+ public function __construct ($ config = [])
53+ {
54+ foreach ($ config as $ name => $ value ) {
55+ $ this ->{$ name } = $ value ;
56+ }
57+ $ this ->init ();
58+ }
59+
60+ public function init (){
61+ $ this ->signer = new ShaHmac1Signer ();
62+ }
63+
64+ /**
65+ * 获取Http Client
66+ * @return \GuzzleHttp\Client
67+ */
68+ public function getHttpClient ()
69+ {
70+ if (!is_object ($ this ->_httpClient )) {
71+ $ this ->_httpClient = new \GuzzleHttp \Client ([
72+ 'verify ' => false ,
73+ 'http_errors ' => false ,
74+ 'connect_timeout ' => 3 ,
75+ 'read_timeout ' => 10 ,
76+ 'debug ' => false ,
77+ ]);
78+ }
79+ return $ this ->_httpClient ;
80+ }
81+
82+
83+ /**
84+ * @param array $params
85+ * @return string
86+ */
87+ public function createRequest (array $ params )
88+ {
89+ $ params ['Format ' ] = 'JSON ' ;
90+ $ params ['Version ' ] = '2016-11-01 ' ;
91+ $ params ['AccessKeyId ' ] = $ this ->accessKeyId ;
92+ $ params ['SignatureMethod ' ] = $ this ->signer ->getSignatureMethod ();
93+ $ params ['Timestamp ' ] = gmdate ('Y-m-d\TH:i:s\Z ' );
94+ $ params ['SignatureVersion ' ] = $ this ->signer ->getSignatureVersion ();
95+ $ params ['SignatureNonce ' ] = uniqid ();
96+ //签名
97+ $ params ['Signature ' ] = $ this ->computeSignature ($ params );
98+ $ requestUrl = $ this ->composeUrl ('http://dm.aliyuncs.com/ ' , $ params );
99+ $ response = $ this ->sendRequest ('GET ' , $ requestUrl );
100+ return $ response ->getBody ()->getContents ();
101+ }
102+
103+ /**
104+ * Sends HTTP request.
105+ * @param string $method request type.
106+ * @param string $url request URL.
107+ * @param array $options request params.
108+ * @return object response.
109+ */
110+ public function sendRequest ($ method , $ url , array $ options = [])
111+ {
112+ $ response = $ request = $ this ->getHttpClient ()->request ($ method , $ url , $ options );
113+ return $ response ;
114+ }
115+
116+ /**
117+ * 合并基础URL和参数
118+ * @param string $url base URL.
119+ * @param array $params GET params.
120+ * @return string composed URL.
121+ */
122+ protected function composeUrl ($ url , array $ params = [])
123+ {
124+ if (strpos ($ url , '? ' ) === false ) {
125+ $ url .= '? ' ;
126+ } else {
127+ $ url .= '& ' ;
128+ }
129+ $ url .= http_build_query ($ params , '' , '& ' , PHP_QUERY_RFC3986 );
130+ return $ url ;
131+ }
132+
133+ /**
134+ * @param array $parameters
135+ * @return string
136+ */
137+ private function computeSignature ($ parameters )
138+ {
139+ ksort ($ parameters );
140+ $ canonicalizedQueryString = '' ;
141+ foreach ($ parameters as $ key => $ value ) {
142+ $ canonicalizedQueryString .= '& ' . $ this ->percentEncode ($ key ) . '= ' . $ this ->percentEncode ($ value );
143+ }
144+ $ stringToSign = 'GET&%2F& ' . $ this ->percentencode (substr ($ canonicalizedQueryString , 1 ));
145+ $ signature = $ this ->signer ->signString ($ stringToSign , $ this ->accessSecret . "& " );
146+
147+ return $ signature ;
148+ }
149+
150+ protected function percentEncode ($ str )
151+ {
152+ $ res = urlencode ($ str );
153+ $ res = preg_replace ('/\+/ ' , '%20 ' , $ res );
154+ $ res = preg_replace ('/\*/ ' , '%2A ' , $ res );
155+ $ res = preg_replace ('/%7E/ ' , '~ ' , $ res );
156+ return $ res ;
157+ }
158+ }
0 commit comments