Skip to content

Commit 7f67c80

Browse files
committed
更新
1 parent a65ca98 commit 7f67c80

File tree

4 files changed

+23
-190
lines changed

4 files changed

+23
-190
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-
"guzzlehttp/guzzle": "~6.0"
23+
"aliyunapi/guzzle-subscriber": "~1.0"
2424
},
2525
"require-dev": {
2626
"phpunit/phpunit": "4.*"

src/Client.php

Lines changed: 22 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,14 @@
2121

2222
namespace aliyun\live;
2323

24-
use aliyun\live\auth\ShaHmac1Signer;
24+
use GuzzleHttp\HandlerStack;
25+
use GuzzleHttp\Client as HttpClient;
26+
use aliyun\guzzle\subscriber\Rpc;
2527

28+
/**
29+
* Class Client
30+
* @package aliyun\live
31+
*/
2632
class Client
2733
{
2834
/**
@@ -35,17 +41,12 @@ class Client
3541
*/
3642
public $accessSecret;
3743

38-
/**
39-
* @var \aliyun\core\auth\SignerInterface 签名算法实例
40-
*/
41-
public $signer;
42-
43-
44+
public $baseUri = 'http://live.aliyuncs.com/';
4445

4546
/**
46-
* @var \GuzzleHttp\Client
47+
* @var HttpClient
4748
*/
48-
public $_httpClient;
49+
private $_httpClient;
4950

5051
/**
5152
* Request constructor.
@@ -56,21 +57,25 @@ public function __construct($config = [])
5657
foreach ($config as $name => $value) {
5758
$this->{$name} = $value;
5859
}
59-
$this->init();
60-
}
61-
62-
public function init(){
63-
$this->signer = new ShaHmac1Signer();
6460
}
6561

6662
/**
6763
* 获取Http Client
68-
* @return \GuzzleHttp\Client
64+
* @return HttpClient
6965
*/
7066
public function getHttpClient()
7167
{
7268
if (!is_object($this->_httpClient)) {
73-
$this->_httpClient = new \GuzzleHttp\Client([
69+
$stack = HandlerStack::create();
70+
$middleware = new Rpc([
71+
'accessKeyId' => $this->accessKeyId,
72+
'accessSecret' => $this->accessSecret,
73+
]);
74+
$stack->push($middleware);
75+
76+
$this->_httpClient = new HttpClient([
77+
'base_uri' => $this->baseUri,
78+
'handler' => $stack,
7479
'verify' => false,
7580
'http_errors' => false,
7681
'connect_timeout' => 3,
@@ -81,80 +86,12 @@ public function getHttpClient()
8186
return $this->_httpClient;
8287
}
8388

84-
8589
/**
8690
* @param array $params
8791
* @return string
8892
*/
8993
public function createRequest(array $params)
9094
{
91-
$params['Format'] = 'JSON';
92-
$params['Version'] = '2016-11-01';
93-
$params['AccessKeyId'] = $this->accessKeyId;
94-
$params['SignatureMethod'] = $this->signer->getSignatureMethod();
95-
$params['Timestamp'] = gmdate('Y-m-d\TH:i:s\Z');
96-
$params['SignatureVersion'] = $this->signer->getSignatureVersion();
97-
$params['SignatureNonce'] = uniqid();
98-
//签名
99-
$params['Signature'] = $this->computeSignature($params);
100-
$requestUrl = $this->composeUrl('http://live.aliyuncs.com/', $params);
101-
$response = $this->sendRequest('GET', $requestUrl);
102-
return $response->getBody()->getContents();
103-
}
104-
105-
/**
106-
* Sends HTTP request.
107-
* @param string $method request type.
108-
* @param string $url request URL.
109-
* @param array $options request params.
110-
* @return object response.
111-
*/
112-
public function sendRequest($method, $url, array $options = [])
113-
{
114-
$response = $request = $this->getHttpClient()->request($method, $url, $options);
115-
return $response;
116-
}
117-
118-
/**
119-
* 合并基础URL和参数
120-
* @param string $url base URL.
121-
* @param array $params GET params.
122-
* @return string composed URL.
123-
*/
124-
protected function composeUrl($url, array $params = [])
125-
{
126-
if (strpos($url, '?') === false) {
127-
$url .= '?';
128-
} else {
129-
$url .= '&';
130-
}
131-
$url .= http_build_query($params, '', '&', PHP_QUERY_RFC3986);
132-
return $url;
133-
}
134-
135-
/**
136-
* @param array $parameters
137-
* @return string
138-
*/
139-
private function computeSignature($parameters)
140-
{
141-
ksort($parameters);
142-
$canonicalizedQueryString = '';
143-
foreach ($parameters as $key => $value) {
144-
$canonicalizedQueryString .= '&' . $this->percentEncode($key) . '=' . $this->percentEncode($value);
145-
}
146-
$stringToSign = 'GET&%2F&' . $this->percentencode(substr($canonicalizedQueryString, 1));
147-
$signature = $this->signer->signString($stringToSign, $this->accessSecret . "&");
148-
149-
return $signature;
150-
}
151-
152-
protected function percentEncode($str)
153-
{
154-
$res = urlencode($str);
155-
$res = preg_replace('/\+/', '%20', $res);
156-
$res = preg_replace('/\*/', '%2A', $res);
157-
$res = preg_replace('/%7E/', '~', $res);
158-
return $res;
95+
return $this->getHttpClient()->get('/',['query'=>$params]);
15996
}
16097
}

src/auth/ShaHmac1Signer.php

Lines changed: 0 additions & 57 deletions
This file was deleted.

src/auth/SignerInterface.php

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)