-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlibabaSms.php
191 lines (175 loc) · 5.45 KB
/
AlibabaSms.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
/**
* Copyright (c) 2020
* @file AlibabaSms.php
* @author USPilot
* @date 2020/10/14
* @time 16:04
*
*/
namespace USPilot;
use \AlibabaCloud\Client\AlibabaCloud;
use \AlibabaCloud\Client\Exception\ClientException;
use \AlibabaCloud\Client\Exception\ServerException;
use Exception;
use \IniParser\IniParser;
use \RosterInfo\proxy;
class AlibabaSms
{
public $PhoneNumbers;
public $SignNames = [];
public $Templates = [];
public $TemplateParam = [];
public $OutId;
public $SmsUpExtendCode;
public $RegionId;
public $result = [];
private $useProxy = false;
private $proxy = null;
private $ActiveTemplate = '';
private $ActiveSignName = '';
/**
* AlibabaSms constructor.
* @param string $iniFile
* @param bool $useProxy
* @throws Exception
*/
public function __construct(string $iniFile = 'ini/sms.php', bool $useProxy = false)
{
$this->Init($iniFile, $useProxy);
}
/**
* @param string $iniFile
* @param bool $useProxy
* @return AlibabaSms
* @throws Exception
*/
public function Init(string $iniFile = 'ini/sms.php', bool $useProxy = false)
{
$this->PhoneNumbers = '';
$this->ActiveSignName = '';
$this->ActiveTemplate = '';
$this->OutId = '';
$this->result = [];
$this->SmsUpExtendCode = [];
$this->TemplateParam = [];
if (!file_exists($iniFile)) throw new Exception('INI file not found',9001);
$iniParser = new IniParser($iniFile);
$ini = $iniParser->parse();
if (empty($ini['SignName'])) throw new Exception('Alibaba `SignName` is missing in INI file',9002);
if (empty($ini['Template'])) throw new Exception('Alibaba `Template` is missing in INI file',9002);
if (empty($ini['AccessKeyId'])) throw new Exception('Alibaba `AccessKeyId` is missing in INI file',9002);
if (empty($ini['AccessKeySecret'])) throw new Exception('Alibaba `AccessKeySecret` is missing in INI file',9002);
if (empty($ini['RegionId'])) $ini['RegionId'] = 'cn-hangzhou';
$this->SignNames = $ini['SignName'];
$this->Templates = $ini['Template'];
$this->setUseProxy($useProxy);
AlibabaCloud::accessKeyClient($ini['AccessKeyId'], $ini['AccessKeySecret'])
->regionId($ini['RegionId'])
->asDefaultClient();
if ($this->useProxy) AlibabaCloud::getDefaultClient()->proxy($this->proxy);
return $this;
}
/**
* @return array
* @throws ClientException
* @throws ServerException
*/
public function sendSms()
{
$res = AlibabaCloud::rpc()
->product('Dysmsapi')
->version('2017-05-25')
->action('SendSms')
->method('POST')
->options([
'query' => [
'RegionId' => $this->RegionId,
'PhoneNumbers' => $this->PhoneNumbers,
'SignName' => $this->ActiveSignName,
'TemplateCode' => $this->ActiveTemplate,
'TemplateParam' => empty($this->TemplateParam)? '' : json_encode($this->TemplateParam),
'SmsUpExtendCode' => empty($this->SmsUpExtendCode)? '' : $this->SmsUpExtendCode,
'OutId' => empty($this->OutId)? '' : $this->OutId,
],
])
->request();
$this->result = $res->toArray();
return $this->result;
}
/**
* @param bool $useProxy
* @param string $type
* @return AlibabaSms
*/
public function setUseProxy($useProxy = true, $type = 'http')
{
if ($useProxy) {
proxy::checkProxy(false);
if ($type != 'socks5') {
$this->proxy = empty(proxy::$host)? null : proxy::$user.'@'.proxy::$host;
}
else {
seld::$proxy = empty(proxy::$socks5Host)? null : 'socks5://'.proxy::$socks5User.'@'.proxy::$socks5Host;
}
} else {
$this->proxy = null;
}
$this->useProxy = $this->proxy? true : false;
return $this;
}
/**
* @param string $ActiveSignName
* @return AlibabaSms
*/
public function setSignName(string $ActiveSignName)
{
$this->ActiveSignName = empty($this->SignNames[$ActiveSignName])? '' : $this->SignNames[$ActiveSignName];
return $this;
}
/**
* @param string $ActiveTemplate
* @return AlibabaSms
*/
public function setTemplate(string $ActiveTemplate)
{
$this->ActiveTemplate = empty($this->Templates[$ActiveTemplate])? '' : $this->Templates[$ActiveTemplate];
return $this;
}
/**
* @param array $TemplateParam
* @return AlibabaSms
*/
public function setTemplateParam(array $TemplateParam)
{
$this->TemplateParam = $TemplateParam;
return $this;
}
/**
* @param mixed $OutId
* @return AlibabaSms
*/
public function setOutId($OutId)
{
$this->OutId = $OutId;
return $this;
}
/**
* @param mixed $SmsUpExtendCode
* @return AlibabaSms
*/
public function setSmsUpExtendCode($SmsUpExtendCode)
{
$this->SmsUpExtendCode = $SmsUpExtendCode;
return $this;
}
/**
* @param mixed $PhoneNumbers
* @return AlibabaSms
*/
public function setPhoneNumbers($PhoneNumbers)
{
$this->PhoneNumbers = $PhoneNumbers;
return $this;
}
}