21
21
22
22
namespace aliyun \dm ;
23
23
24
- use aliyun \dm \auth \ShaHmac1Signer ;
24
+ use GuzzleHttp \HandlerStack ;
25
+ use GuzzleHttp \Client as HttpClient ;
26
+ use aliyun \guzzle \subscriber \Rpc ;
25
27
28
+ /**
29
+ * Class Client
30
+ * @package aliyun\dm
31
+ */
26
32
class Client
27
33
{
28
34
/**
@@ -36,14 +42,19 @@ class Client
36
42
public $ accessSecret ;
37
43
38
44
/**
39
- * @var \aliyun\core\auth\SignerInterface 签名算法实例
45
+ * @var string API版本
46
+ */
47
+ public $ version = '2015-11-23 ' ;
48
+
49
+ /**
50
+ * @var string 网关地址
40
51
*/
41
- public $ signer ;
52
+ public $ baseUri = ' http://dm.aliyuncs.com/ ' ;
42
53
43
54
/**
44
- * @var \GuzzleHttp\Client
55
+ * @var HttpClient
45
56
*/
46
- public $ _httpClient ;
57
+ private $ _httpClient ;
47
58
48
59
/**
49
60
* Request constructor.
@@ -54,21 +65,26 @@ public function __construct($config = [])
54
65
foreach ($ config as $ name => $ value ) {
55
66
$ this ->{$ name } = $ value ;
56
67
}
57
- $ this ->init ();
58
- }
59
-
60
- public function init (){
61
- $ this ->signer = new ShaHmac1Signer ();
62
68
}
63
69
64
70
/**
65
71
* 获取Http Client
66
- * @return \GuzzleHttp\Client
72
+ * @return HttpClient
67
73
*/
68
74
public function getHttpClient ()
69
75
{
70
76
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 ,
72
88
'verify ' => false ,
73
89
'http_errors ' => false ,
74
90
'connect_timeout ' => 3 ,
@@ -79,80 +95,12 @@ public function getHttpClient()
79
95
return $ this ->_httpClient ;
80
96
}
81
97
82
-
83
98
/**
84
99
* @param array $params
85
100
* @return string
86
101
*/
87
102
public function createRequest (array $ params )
88
103
{
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 ]);
157
105
}
158
106
}
0 commit comments