21
21
22
22
namespace aliyun \live ;
23
23
24
- class Client {
25
-
24
+ class Client
25
+ {
26
+ public $ secretId ;
27
+ public $ secretKey ;
28
+
29
+ public $ version = '2016-11-01 ' ;
30
+
31
+ /**
32
+ * @var string 时间格式
33
+ */
34
+ public $ dateTimeFormat = 'Y-m-d\TH:i:s\Z ' ;
35
+
36
+ /**
37
+ * Client constructor.
38
+ * @param string $secretId
39
+ * @param string $secretKey
40
+ */
41
+ public function __construct ($ secretId , $ secretKey )
42
+ {
43
+ $ this ->secretId = $ secretId ;
44
+ $ this ->secretKey = $ secretKey ;
45
+ }
46
+
47
+ public function request (array $ params )
48
+ {
49
+ $ 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 ' ;
55
+ $ params ['SignatureNonce ' ] = uniqid ();
56
+ $ plainText = $ this ->makeSignPlainText ($ params );
57
+ //签名
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 );
76
+ return $ response ->getBody ()->getContents ();
77
+ }
78
+
79
+ /**
80
+ * @param $parameters
81
+ * @return mixed
82
+ */
83
+ private function makeSignPlainText ($ parameters )
84
+ {
85
+ ksort ($ parameters );
86
+ $ canonicalizedQueryString = '' ;
87
+ foreach ($ parameters as $ key => $ value ) {
88
+ $ canonicalizedQueryString .= '& ' . $ this ->percentEncode ($ key ) . '= ' . $ this ->percentEncode ($ value );
89
+ }
90
+ return $ canonicalizedQueryString ;
91
+ }
92
+
93
+ protected function percentEncode ($ str )
94
+ {
95
+ $ res = urlencode ($ str );
96
+ $ res = preg_replace ('/\+/ ' , '%20 ' , $ res );
97
+ $ res = preg_replace ('/\*/ ' , '%2A ' , $ res );
98
+ $ res = preg_replace ('/%7E/ ' , '~ ' , $ res );
99
+ return $ res ;
100
+ }
101
+
26
102
}
0 commit comments