Skip to content

Commit ee71c86

Browse files
committed
init
1 parent c12f625 commit ee71c86

File tree

10 files changed

+412
-0
lines changed

10 files changed

+412
-0
lines changed

.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
; This file is for unifying the coding style for different editors and IDEs.
2+
; More information at http://editorconfig.org
3+
4+
root = true
5+
6+
[*]
7+
charset = utf-8
8+
indent_size = 4
9+
indent_style = space
10+
end_of_line = lf
11+
insert_final_newline = true
12+
trim_trailing_whitespace = true
13+
14+
[*.md]
15+
trim_trailing_whitespace = false

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/.idea
2+
/.DS_Store
3+
/vendor
4+
phpunit.xml
5+
composer.lock

.travis.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
language: php
2+
php:
3+
- 7.1
4+
- 7.0
5+
- 5.6
6+
- 5.5
7+
install:
8+
- composer self-update
9+
- composer install --no-interaction
10+
script:
11+
- php vendor/bin/phpunit

composer.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"name": "aliyunapi/php-aliyun-open-api-dm",
3+
"description": "aliyunsdk",
4+
"keywords": [
5+
"aliyun",
6+
"aliyun sdk",
7+
"dm"
8+
],
9+
"type": "library",
10+
"license": "Apache",
11+
"support": {
12+
"issues": "https://github.com/aliyunapi/php-aliyun-open-api-dm/issues",
13+
"source": "https://github.com/aliyunapi/php-aliyun-open-api-dm"
14+
},
15+
"authors": [
16+
{
17+
"name": "Xu TL",
18+
"email": "xutongle@gmail.com"
19+
}
20+
],
21+
"require": {
22+
"php": ">=5.5.0",
23+
"guzzlehttp/guzzle": "~6.0"
24+
},
25+
"require-dev": {
26+
"phpunit/phpunit": "4.*"
27+
},
28+
"autoload": {
29+
"psr-4": {
30+
"aliyun\\dm\\": "src"
31+
}
32+
},
33+
"autoload-dev": {
34+
"psr-4": {
35+
"aliyun\\test\\dm\\": "tests"
36+
}
37+
},
38+
"scripts": {
39+
"test": "phpunit"
40+
}
41+
}

phpunit.xml.dist

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<phpunit bootstrap="./tests/bootstrap.php"
3+
colors="true"
4+
convertErrorsToExceptions="true"
5+
convertNoticesToExceptions="true"
6+
convertWarningsToExceptions="true"
7+
stopOnFailure="false">
8+
<testsuites>
9+
<testsuite name="Aliyun Test Suite">
10+
<directory>./tests</directory>
11+
</testsuite>
12+
</testsuites>
13+
</phpunit>

src/Client.php

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
<?php
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
namespace aliyun\dm;
23+
24+
use aliyun\dm\auth\ShaHmac1Signer;
25+
26+
class Client
27+
{
28+
/**
29+
* @var string
30+
*/
31+
public $accessKeyId;
32+
33+
/**
34+
* @var string
35+
*/
36+
public $accessSecret;
37+
38+
/**
39+
* @var \aliyun\core\auth\SignerInterface 签名算法实例
40+
*/
41+
public $signer;
42+
43+
/**
44+
* @var \GuzzleHttp\Client
45+
*/
46+
public $_httpClient;
47+
48+
/**
49+
* Request constructor.
50+
* @param array $config
51+
*/
52+
public function __construct($config = [])
53+
{
54+
foreach ($config as $name => $value) {
55+
$this->{$name} = $value;
56+
}
57+
$this->init();
58+
}
59+
60+
public function init(){
61+
$this->signer = new ShaHmac1Signer();
62+
}
63+
64+
/**
65+
* 获取Http Client
66+
* @return \GuzzleHttp\Client
67+
*/
68+
public function getHttpClient()
69+
{
70+
if (!is_object($this->_httpClient)) {
71+
$this->_httpClient = new \GuzzleHttp\Client([
72+
'verify' => false,
73+
'http_errors' => false,
74+
'connect_timeout' => 3,
75+
'read_timeout' => 10,
76+
'debug' => false,
77+
]);
78+
}
79+
return $this->_httpClient;
80+
}
81+
82+
83+
/**
84+
* @param array $params
85+
* @return string
86+
*/
87+
public function createRequest(array $params)
88+
{
89+
$params['Format'] = 'JSON';
90+
$params['Version'] = '2016-11-01';
91+
$params['AccessKeyId'] = $this->accessKeyId;
92+
$params['SignatureMethod'] = $this->signer->getSignatureMethod();
93+
$params['Timestamp'] = gmdate('Y-m-d\TH:i:s\Z');
94+
$params['SignatureVersion'] = $this->signer->getSignatureVersion();
95+
$params['SignatureNonce'] = uniqid();
96+
//签名
97+
$params['Signature'] = $this->computeSignature($params);
98+
$requestUrl = $this->composeUrl('http://dm.aliyuncs.com/', $params);
99+
$response = $this->sendRequest('GET', $requestUrl);
100+
return $response->getBody()->getContents();
101+
}
102+
103+
/**
104+
* Sends HTTP request.
105+
* @param string $method request type.
106+
* @param string $url request URL.
107+
* @param array $options request params.
108+
* @return object response.
109+
*/
110+
public function sendRequest($method, $url, array $options = [])
111+
{
112+
$response = $request = $this->getHttpClient()->request($method, $url, $options);
113+
return $response;
114+
}
115+
116+
/**
117+
* 合并基础URL和参数
118+
* @param string $url base URL.
119+
* @param array $params GET params.
120+
* @return string composed URL.
121+
*/
122+
protected function composeUrl($url, array $params = [])
123+
{
124+
if (strpos($url, '?') === false) {
125+
$url .= '?';
126+
} else {
127+
$url .= '&';
128+
}
129+
$url .= http_build_query($params, '', '&', PHP_QUERY_RFC3986);
130+
return $url;
131+
}
132+
133+
/**
134+
* @param array $parameters
135+
* @return string
136+
*/
137+
private function computeSignature($parameters)
138+
{
139+
ksort($parameters);
140+
$canonicalizedQueryString = '';
141+
foreach ($parameters as $key => $value) {
142+
$canonicalizedQueryString .= '&' . $this->percentEncode($key) . '=' . $this->percentEncode($value);
143+
}
144+
$stringToSign = 'GET&%2F&' . $this->percentencode(substr($canonicalizedQueryString, 1));
145+
$signature = $this->signer->signString($stringToSign, $this->accessSecret . "&");
146+
147+
return $signature;
148+
}
149+
150+
protected function percentEncode($str)
151+
{
152+
$res = urlencode($str);
153+
$res = preg_replace('/\+/', '%20', $res);
154+
$res = preg_replace('/\*/', '%2A', $res);
155+
$res = preg_replace('/%7E/', '~', $res);
156+
return $res;
157+
}
158+
}

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\dm\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\dm\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)