2121
2222namespace aliyun \dm ;
2323
24- use aliyun \dm \auth \ShaHmac1Signer ;
24+ use GuzzleHttp \HandlerStack ;
25+ use GuzzleHttp \Client as HttpClient ;
26+ use aliyun \guzzle \subscriber \Rpc ;
2527
28+ /**
29+ * Class Client
30+ * @package aliyun\dm
31+ */
2632class Client
2733{
2834 /**
@@ -36,14 +42,19 @@ class Client
3642 public $ accessSecret ;
3743
3844 /**
39- * @var \aliyun\core\auth\SignerInterface 签名算法实例
45+ * @var string API版本
46+ */
47+ public $ version = '2015-11-23 ' ;
48+
49+ /**
50+ * @var string 网关地址
4051 */
41- public $ signer ;
52+ public $ baseUri = ' http://dm.aliyuncs.com/ ' ;
4253
4354 /**
44- * @var \GuzzleHttp\Client
55+ * @var HttpClient
4556 */
46- public $ _httpClient ;
57+ private $ _httpClient ;
4758
4859 /**
4960 * Request constructor.
@@ -54,21 +65,26 @@ public function __construct($config = [])
5465 foreach ($ config as $ name => $ value ) {
5566 $ this ->{$ name } = $ value ;
5667 }
57- $ this ->init ();
58- }
59-
60- public function init (){
61- $ this ->signer = new ShaHmac1Signer ();
6268 }
6369
6470 /**
6571 * 获取Http Client
66- * @return \GuzzleHttp\Client
72+ * @return HttpClient
6773 */
6874 public function getHttpClient ()
6975 {
7076 if (!is_object ($ this ->_httpClient )) {
71- $ this ->_httpClient = new \GuzzleHttp \Client ([
77+ $ stack = HandlerStack::create ();
78+ $ middleware = new Rpc ([
79+ 'accessKeyId ' => $ this ->accessKeyId ,
80+ 'accessSecret ' => $ this ->accessSecret ,
81+ 'Version ' => $ this ->version
82+ ]);
83+ $ stack ->push ($ middleware );
84+
85+ $ this ->_httpClient = new HttpClient ([
86+ 'base_uri ' => $ this ->baseUri ,
87+ 'handler ' => $ stack ,
7288 'verify ' => false ,
7389 'http_errors ' => false ,
7490 'connect_timeout ' => 3 ,
@@ -79,80 +95,12 @@ public function getHttpClient()
7995 return $ this ->_httpClient ;
8096 }
8197
82-
8398 /**
8499 * @param array $params
85100 * @return string
86101 */
87102 public function createRequest (array $ params )
88103 {
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 ;
104+ return $ this ->getHttpClient ()->get ('/ ' , ['query ' => $ params ]);
157105 }
158106}
0 commit comments