21
21
22
22
namespace aliyun \live ;
23
23
24
+ use aliyun \live \auth \ShaHmac1Signer ;
25
+
24
26
class Client
25
27
{
26
- public $ secretId ;
27
- public $ secretKey ;
28
+ /**
29
+ * @var string
30
+ */
31
+ public $ accessKeyId ;
28
32
29
- public $ version = '2016-11-01 ' ;
33
+ /**
34
+ * @var string
35
+ */
36
+ public $ accessSecret ;
30
37
31
38
/**
32
- * @var string 时间格式
39
+ * @var \aliyun\core\auth\SignerInterface 签名算法实例
33
40
*/
34
- public $ dateTimeFormat = ' Y-m-d\TH:i:s\Z ' ;
41
+ public $ signer ;
35
42
36
43
/**
37
44
* Client constructor.
38
- * @param string $secretId
39
- * @param string $secretKey
45
+ * @param string $accessKeyId
46
+ * @param string $accessSecret
40
47
*/
41
- public function __construct ($ secretId , $ secretKey )
48
+ public function __construct ($ accessKeyId , $ accessSecret )
42
49
{
43
- $ this ->secretId = $ secretId ;
44
- $ this ->secretKey = $ secretKey ;
50
+ $ this ->accessKeyId = $ accessKeyId ;
51
+ $ this ->accessSecret = $ accessSecret ;
52
+ $ this ->signer = new ShaHmac1Signer ();
45
53
}
46
54
47
- public function request (array $ params )
55
+ /**
56
+ * @var \GuzzleHttp\Client
57
+ */
58
+ public $ _httpClient ;
59
+
60
+ /**
61
+ * 获取Http Client
62
+ * @return \GuzzleHttp\Client
63
+ */
64
+ public function getHttpClient ()
65
+ {
66
+ if (!is_object ($ this ->_httpClient )) {
67
+ $ this ->_httpClient = new \GuzzleHttp \Client ([
68
+ 'verify ' => false ,
69
+ 'http_errors ' => false ,
70
+ 'connect_timeout ' => 3 ,
71
+ 'read_timeout ' => 10 ,
72
+ 'debug ' => false ,
73
+ ]);
74
+ }
75
+ return $ this ->_httpClient ;
76
+ }
77
+
78
+
79
+ /**
80
+ * @param array $params
81
+ * @return string
82
+ */
83
+ public function createRequest (array $ params )
48
84
{
49
85
$ params ['Format ' ] = 'JSON ' ;
50
- $ params ['Version ' ] = $ this -> version ;
51
- $ params ['AccessKeyId ' ] = $ this ->secretId ;
52
- $ params ['SignatureMethod ' ] = ' HMAC-SHA1 ' ;
53
- $ params ['Timestamp ' ] = gmdate ($ this -> dateTimeFormat );
54
- $ params ['SignatureVersion ' ] = ' 1.0 ' ;
86
+ $ params ['Version ' ] = ' 2016-11-01 ' ;
87
+ $ params ['AccessKeyId ' ] = $ this ->accessKeyId ;
88
+ $ params ['SignatureMethod ' ] = $ this -> signer -> getSignatureMethod () ;
89
+ $ params ['Timestamp ' ] = gmdate (' Y-m-d\TH:i:s\Z ' );
90
+ $ params ['SignatureVersion ' ] = $ this -> signer -> getSignatureVersion () ;
55
91
$ params ['SignatureNonce ' ] = uniqid ();
56
- $ plainText = $ this ->makeSignPlainText ($ params );
57
92
//签名
58
- $ plainText = 'GET&%2F& ' . $ this ->percentencode (substr ($ plainText , 1 ));
59
- $ params ['Signature ' ] = base64_encode (hash_hmac ('sha1 ' , $ plainText , $ this ->secretKey . "& " , true ));
60
- $ client = new \GuzzleHttp \Client ([
61
- 'verify ' => false ,
62
- 'http_errors ' => false ,
63
- 'connect_timeout ' => 3 ,
64
- 'read_timeout ' => 10 ,
65
- 'debug ' => true ,
66
- ]);
67
- $ requestUrl = 'http://live.aliyuncs.com/? ' ;
68
- foreach ($ params as $ apiParamKey => $ apiParamValue ) {
69
- $ requestUrl .= "$ apiParamKey= " . urlencode ($ apiParamValue ) . "& " ;
70
- }
71
- $ requestUrl = substr ($ requestUrl , 0 , -1 );
72
- $ request = new \GuzzleHttp \Psr7 \Request ('GET ' , $ requestUrl , [
73
- 'client ' => 'php/1.0.0 ' ,
74
- ]);
75
- $ response = $ client ->send ($ request );
93
+ $ params ['Signature ' ] = $ this ->computeSignature ($ params );
94
+ $ requestUrl = $ this ->composeUrl ('http://live.aliyuncs.com/ ' , $ params );
95
+ $ response = $ this ->sendRequest ('GET ' , $ requestUrl );
76
96
return $ response ->getBody ()->getContents ();
77
97
}
78
98
79
99
/**
80
- * @param $parameters
81
- * @return mixed
100
+ * Sends HTTP request.
101
+ * @param string $method request type.
102
+ * @param string $url request URL.
103
+ * @param array $options request params.
104
+ * @return object response.
105
+ */
106
+ public function sendRequest ($ method , $ url , array $ options = [])
107
+ {
108
+ $ response = $ request = $ this ->getHttpClient ()->request ($ method , $ url , $ options );
109
+ return $ response ;
110
+ }
111
+
112
+ /**
113
+ * 合并基础URL和参数
114
+ * @param string $url base URL.
115
+ * @param array $params GET params.
116
+ * @return string composed URL.
82
117
*/
83
- private function makeSignPlainText ($ parameters )
118
+ protected function composeUrl ($ url , array $ params = [])
119
+ {
120
+ if (strpos ($ url , '? ' ) === false ) {
121
+ $ url .= '? ' ;
122
+ } else {
123
+ $ url .= '& ' ;
124
+ }
125
+ $ url .= http_build_query ($ params , '' , '& ' , PHP_QUERY_RFC3986 );
126
+ return $ url ;
127
+ }
128
+
129
+ /**
130
+ * @param array $parameters
131
+ * @return string
132
+ */
133
+ private function computeSignature ($ parameters )
84
134
{
85
135
ksort ($ parameters );
86
136
$ canonicalizedQueryString = '' ;
87
137
foreach ($ parameters as $ key => $ value ) {
88
138
$ canonicalizedQueryString .= '& ' . $ this ->percentEncode ($ key ) . '= ' . $ this ->percentEncode ($ value );
89
139
}
90
- return $ canonicalizedQueryString ;
140
+ $ stringToSign = 'GET&%2F& ' . $ this ->percentencode (substr ($ canonicalizedQueryString , 1 ));
141
+ $ signature = $ this ->signer ->signString ($ stringToSign , $ this ->accessSecret . "& " );
142
+
143
+ return $ signature ;
91
144
}
92
145
93
146
protected function percentEncode ($ str )
@@ -98,5 +151,4 @@ protected function percentEncode($str)
98
151
$ res = preg_replace ('/%7E/ ' , '~ ' , $ res );
99
152
return $ res ;
100
153
}
101
-
102
154
}
0 commit comments