2121
2222namespace aliyun \live ;
2323
24+ use aliyun \live \auth \ShaHmac1Signer ;
25+
2426class Client
2527{
26- public $ secretId ;
27- public $ secretKey ;
28+ /**
29+ * @var string
30+ */
31+ public $ accessKeyId ;
2832
29- public $ version = '2016-11-01 ' ;
33+ /**
34+ * @var string
35+ */
36+ public $ accessSecret ;
3037
3138 /**
32- * @var string 时间格式
39+ * @var \aliyun\core\auth\SignerInterface 签名算法实例
3340 */
34- public $ dateTimeFormat = ' Y-m-d\TH:i:s\Z ' ;
41+ public $ signer ;
3542
3643 /**
3744 * Client constructor.
38- * @param string $secretId
39- * @param string $secretKey
45+ * @param string $accessKeyId
46+ * @param string $accessSecret
4047 */
41- public function __construct ($ secretId , $ secretKey )
48+ public function __construct ($ accessKeyId , $ accessSecret )
4249 {
43- $ this ->secretId = $ secretId ;
44- $ this ->secretKey = $ secretKey ;
50+ $ this ->accessKeyId = $ accessKeyId ;
51+ $ this ->accessSecret = $ accessSecret ;
52+ $ this ->signer = new ShaHmac1Signer ();
4553 }
4654
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 )
4884 {
4985 $ 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 () ;
5591 $ params ['SignatureNonce ' ] = uniqid ();
56- $ plainText = $ this ->makeSignPlainText ($ params );
5792 //签名
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 );
7696 return $ response ->getBody ()->getContents ();
7797 }
7898
7999 /**
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.
82117 */
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 )
84134 {
85135 ksort ($ parameters );
86136 $ canonicalizedQueryString = '' ;
87137 foreach ($ parameters as $ key => $ value ) {
88138 $ canonicalizedQueryString .= '& ' . $ this ->percentEncode ($ key ) . '= ' . $ this ->percentEncode ($ value );
89139 }
90- return $ canonicalizedQueryString ;
140+ $ stringToSign = 'GET&%2F& ' . $ this ->percentencode (substr ($ canonicalizedQueryString , 1 ));
141+ $ signature = $ this ->signer ->signString ($ stringToSign , $ this ->accessSecret . "& " );
142+
143+ return $ signature ;
91144 }
92145
93146 protected function percentEncode ($ str )
@@ -98,5 +151,4 @@ protected function percentEncode($str)
98151 $ res = preg_replace ('/%7E/ ' , '~ ' , $ res );
99152 return $ res ;
100153 }
101-
102154}
0 commit comments