Skip to content

Commit 859e6e4

Browse files
committed
先临时用着,官方的过度设计,垃圾
1 parent 160690b commit 859e6e4

File tree

4 files changed

+197
-41
lines changed

4 files changed

+197
-41
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
],
2121
"require": {
2222
"php": ">=5.5.0",
23-
"aliyunapi/php-aliyun-open-api-core": "~1.0 | dev-master"
23+
"guzzlehttp/guzzle": "~6.0"
2424
},
2525
"require-dev": {
2626
"phpunit/phpunit": "4.*"

src/Client.php

Lines changed: 92 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -21,73 +21,126 @@
2121

2222
namespace aliyun\live;
2323

24+
use aliyun\live\auth\ShaHmac1Signer;
25+
2426
class 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
}

src/auth/ShaHmac1Signer.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/*
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
*/
20+
namespace aliyun\live\auth;
21+
22+
/**
23+
* Class ShaHmac1Signer
24+
* @package aliyun\core\auth
25+
*/
26+
class ShaHmac1Signer implements SignerInterface
27+
{
28+
/**
29+
* 对字符串进行签名
30+
* @param string $source
31+
* @param string $accessSecret
32+
* @return string
33+
*/
34+
public function signString($source, $accessSecret)
35+
{
36+
return base64_encode(hash_hmac('sha1', $source, $accessSecret, true));
37+
}
38+
39+
/**
40+
* 获取签名方法
41+
* @return string
42+
*/
43+
public function getSignatureMethod()
44+
{
45+
return 'HMAC-SHA1';
46+
}
47+
48+
/**
49+
* 获取版本
50+
* @return string
51+
*/
52+
public function getSignatureVersion()
53+
{
54+
return '1.0';
55+
}
56+
57+
}

src/auth/SignerInterface.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/*
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
*/
20+
namespace aliyun\live\auth;
21+
22+
/**
23+
* 签名接口
24+
* @package aliyun\core\auth
25+
*/
26+
interface SignerInterface
27+
{
28+
/**
29+
* 获取签名方法
30+
* @return string
31+
*/
32+
public function getSignatureMethod();
33+
34+
/**
35+
* 获取签名版本
36+
* @return string
37+
*/
38+
public function getSignatureVersion();
39+
40+
/**
41+
* 对字符串进行签名
42+
* @param string $source
43+
* @param string $accessSecret
44+
* @return string
45+
*/
46+
public function signString($source, $accessSecret);
47+
}

0 commit comments

Comments
 (0)