66 */
77namespace aliyun \live ;
88
9- class Request
9+ use aliyun \core \Base ;
10+ use aliyun \core \auth \ShaHmac1Signer ;
11+
12+ class Request extends Base
1013{
11- protected $ queryParameters = [
14+ public $ accessKeyId ;
15+
16+ public $ accessSecret ;
17+
18+
19+
20+ /**
21+ * @var \GuzzleHttp\Client
22+ */
23+ public $ _httpClient ;
1224
13- ];
25+ public function init (){
26+ parent ::init ();
1427
15- public function __construct ($ p )
28+ }
29+
30+ /**
31+ * 获取Http Client
32+ * @return \GuzzleHttp\Client
33+ */
34+ public function getHttpClient ()
1635 {
17- $ this ->queryParameters = [
18- 'Format ' => 'JSON ' ,
19- 'Version ' => '2016-11-01 ' ,
20- 'Timestamp ' => gmdate ('Y-m-d\TH:i:s\Z ' ),
21- 'SignatureNonce ' => uniqid (),
22- ];
36+ if (!is_object ($ this ->_httpClient )) {
37+ $ this ->_httpClient = new \GuzzleHttp \Client ([
38+ 'verify ' => false ,
39+ 'http_errors ' => false ,
40+ 'connect_timeout ' => 3 ,
41+ 'read_timeout ' => 10 ,
42+ 'debug ' => false ,
43+ ]);
44+ }
45+ return $ this ->_httpClient ;
2346 }
2447
2548 /**
@@ -46,7 +69,37 @@ public function __call($name, $arguments)
4669 */
4770 protected function _dispatchRequest ($ params )
4871 {
72+ $ params ['Format ' ] = 'JSON ' ;
73+ $ params ['Version ' ] = '2016-11-01 ' ;
74+ $ params ['AccessKeyId ' ] = $ this ->accessKeyId ;
75+ $ params ['SignatureMethod ' ] = 'HMAC-SHA1 ' ;
76+ $ params ['Timestamp ' ] = gmdate ('Y-m-d\TH:i:s\Z ' );
77+ $ params ['SignatureVersion ' ] = '1.0 ' ;
78+ $ params ['SignatureNonce ' ] = uniqid ();
79+ //签名
80+ $ params ['Signature ' ] = $ this ->computeSignature ($ params );
81+ $ requestUrl = $ this ->composeUrl ('http://live.aliyuncs.com/ ' , $ params );
82+
83+ $ response = $ request = $ this ->getHttpClient ()->request ('GET ' , $ requestUrl );
84+ return $ response ;
85+
86+ }
4987
88+ /**
89+ * 合并基础URL和参数
90+ * @param string $url base URL.
91+ * @param array $params GET params.
92+ * @return string composed URL.
93+ */
94+ protected function composeUrl ($ url , array $ params = [])
95+ {
96+ if (strpos ($ url , '? ' ) === false ) {
97+ $ url .= '? ' ;
98+ } else {
99+ $ url .= '& ' ;
100+ }
101+ $ url .= http_build_query ($ params , '' , '& ' , PHP_QUERY_RFC3986 );
102+ return $ url ;
50103 }
51104
52105 /**
@@ -61,7 +114,7 @@ private function computeSignature($parameters)
61114 $ canonicalizedQueryString .= '& ' . $ this ->percentEncode ($ key ) . '= ' . $ this ->percentEncode ($ value );
62115 }
63116 $ stringToSign = 'GET&%2F& ' . $ this ->percentencode (substr ($ canonicalizedQueryString , 1 ));
64- $ signature = $ this -> signer -> signString ( $ stringToSign , $ this ->accessSecret . "& " );
117+ $ signature = base64_encode ( hash_hmac ( ' sha1 ' , $ stringToSign , $ this ->accessSecret . "& " , true ) );
65118
66119 return $ signature ;
67120 }
0 commit comments