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