|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the overtrue/easy-sms. |
|
5
|
|
|
* (c) overtrue <[email protected]> |
|
6
|
|
|
* This source file is subject to the MIT license that is bundled |
|
7
|
|
|
* with this source code in the file LICENSE. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Overtrue\EasySms\Gateways; |
|
11
|
|
|
|
|
12
|
|
|
use Overtrue\EasySms\Contracts\MessageInterface; |
|
13
|
|
|
use Overtrue\EasySms\Exceptions\GatewayErrorException; |
|
14
|
|
|
use Overtrue\EasySms\Support\Config; |
|
15
|
|
|
use Overtrue\EasySms\Traits\HasHttpRequest; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class AliyunGateway. |
|
19
|
|
|
* @author carson <[email protected]> |
|
20
|
|
|
* |
|
21
|
|
|
* @see https://help.aliyun.com/document_detail/55451.html |
|
22
|
|
|
*/ |
|
23
|
|
|
class AliyunGateway extends Gateway |
|
24
|
|
|
{ |
|
25
|
|
|
use HasHttpRequest; |
|
26
|
|
|
|
|
27
|
|
|
const ENDPOINT_URL = 'http://dysmsapi.aliyuncs.com'; |
|
28
|
|
|
const ENDPOINT_METHOD = 'SendSms'; |
|
29
|
|
|
const ENDPOINT_VERSION = '2017-05-25'; |
|
30
|
|
|
const ENDPOINT_FORMAT = 'JSON'; |
|
31
|
|
|
const ENDPOINT_REGION_ID = 'cn-hangzhou'; |
|
32
|
|
|
const ENDPOINT_SIGNATURE_METHOD = 'HMAC-SHA1'; |
|
33
|
|
|
const ENDPOINT_SIGNATURE_VERSION = '1.0'; |
|
34
|
|
|
|
|
35
|
1 |
|
public function __construct(array $config) |
|
36
|
|
|
{ |
|
37
|
1 |
|
parent::__construct($config); |
|
38
|
1 |
|
date_default_timezone_set('GMT'); |
|
39
|
1 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param array|int|string $to |
|
43
|
|
|
* @param \Overtrue\EasySms\Contracts\MessageInterface $message |
|
44
|
|
|
* @param \Overtrue\EasySms\Support\Config $config |
|
45
|
|
|
* |
|
46
|
|
|
* @return array |
|
47
|
|
|
* |
|
48
|
|
|
* @throws \Overtrue\EasySms\Exceptions\GatewayErrorException; |
|
49
|
|
|
*/ |
|
50
|
1 |
|
public function send($to, MessageInterface $message, Config $config) |
|
51
|
|
|
{ |
|
52
|
|
|
$params = [ |
|
53
|
1 |
|
'RegionId' => self::ENDPOINT_REGION_ID, |
|
54
|
1 |
|
'AccessKeyId' => $config->get('access_key_id'), |
|
55
|
1 |
|
'Format' => self::ENDPOINT_FORMAT, |
|
56
|
1 |
|
'SignatureMethod' => self::ENDPOINT_SIGNATURE_METHOD, |
|
57
|
1 |
|
'SignatureVersion' => self::ENDPOINT_SIGNATURE_VERSION, |
|
58
|
1 |
|
'SignatureNonce' => uniqid(), |
|
59
|
1 |
|
'Timestamp' => $this->getTimestamp(), |
|
60
|
1 |
|
'Action' => self::ENDPOINT_METHOD, |
|
61
|
1 |
|
'Version' => self::ENDPOINT_VERSION, |
|
62
|
1 |
|
'PhoneNumbers' => strval($to), |
|
63
|
1 |
|
'SignName' => $config->get('sign_name'), |
|
64
|
1 |
|
'TemplateCode' => $message->getTemplate(), |
|
65
|
1 |
|
'TemplateParam' => json_encode($message->getData()), |
|
66
|
1 |
|
]; |
|
67
|
|
|
|
|
68
|
1 |
|
$params['Signature'] = $this->generateSign($params); |
|
69
|
|
|
|
|
70
|
1 |
|
$result = $this->get(self::ENDPOINT_URL, $params); |
|
71
|
|
|
|
|
72
|
1 |
View Code Duplication |
if ($result['Code'] != 'OK') { |
|
|
|
|
|
|
73
|
1 |
|
throw new GatewayErrorException($result['Message'], $result['Code'], $result); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
1 |
|
return $result; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Generate Sign. |
|
81
|
|
|
* |
|
82
|
|
|
* @param array $params |
|
83
|
|
|
* |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
1 |
|
protected function generateSign($params) |
|
87
|
|
|
{ |
|
88
|
1 |
|
ksort($params); |
|
89
|
1 |
|
$accessKeySecret = $this->config->get('access_key_secret'); |
|
90
|
1 |
|
$stringToSign = 'GET' . '&%2F&' . urlencode(http_build_query($params, null, '&', PHP_QUERY_RFC3986)); |
|
91
|
|
|
|
|
92
|
1 |
|
return base64_encode(hash_hmac('sha1', $stringToSign, $accessKeySecret . '&', true)); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return false|string |
|
97
|
|
|
*/ |
|
98
|
1 |
|
protected function getTimestamp() |
|
99
|
|
|
{ |
|
100
|
1 |
|
$timezone = date_default_timezone_get(); |
|
101
|
1 |
|
date_default_timezone_set('GMT'); |
|
102
|
1 |
|
$timestamp = date('Y-m-d\TH:i:s\Z'); |
|
103
|
1 |
|
date_default_timezone_set($timezone); |
|
104
|
|
|
|
|
105
|
1 |
|
return $timestamp; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.