-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAliDysms.php
648 lines (565 loc) · 20.6 KB
/
AliDysms.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
<?php
/**
* 阿里云 - 短信服务功能模块。
* Class AliDysms
*/
class AliDysms
{
/**
* @var string $accessKeyId 访问密钥 ID 。
*/
private $accessKeyId;
/**
* @var string $accessKeySecret 访问密钥。
*/
private $accessKeySecret;
/**
* 支持的 API 列表:
* 短信发送接口:
* SendSms 发送短信。
* SendBatchSms 批量发送短信。
* 短信查询接口:
* QuerySendDetails 查询短信发送的状态。
* 签名申请接口:
* AddSmsSign 调用短信 AddSmsSign 申请短信签名。
* DeleteSmsSign 调用接口 DeleteSmsSign 删除短信签名。
* QuerySmsSign 调用接口 QuerySmsSign 查询短信签名申请状态。
* ModifySmsSign 调用接口 ModifySmsSign 修改未审核通过的短信签名,并重新提交审核。
* 模板申请接口:
* ModifySmsTemplate 调用接口 ModifySmsTemplate 修改未通过审核的短信模板。
* QuerySmsTemplate 调用接口 QuerySmsTemplate 查询短信模板的审核状态。
* AddSmsTemplate 调用接口 AddSmsTemplate 申请短信模板。
* DeleteSmsTemplate 调用接口 DeleteSmsTemplate 删除短信模板。
* 回执消息:
* SmsReport 订阅 SmsReport 短信状态报告,获取短信发送状态。
* SmsUp 订阅 SmsUp 上行短信消息,获取终端用户回复短信的内容。
* SignSmsReport 订阅签名审核状态消息( SignSmsReport ),获取指定签名的审核状态。
* TemplateSmsReport 订阅模板审核状态消息( TemplateSmsReport ),获取指定模板的审核状态。
*
* @var string $action API 的名称。
*/
private $action;
/**
* @var array $endpoints 阿里云公网服务地址。
*/
private $endpoints = [
'dysmsapi' => [
'global' => 'dysmsapi.aliyuncs.com',
'cn-hangzhou' => 'dysmsapi.aliyuncs.com',
'ap-southeast-1' => 'dysmsapi.ap-southeast-1.aliyuncs.com',
],
'dybaseapi' => [
'global' => 'dybaseapi.aliyuncs.com',
'cn-hangzhou' => '1943695596114318.mns.cn-hangzhou.aliyuncs.com', // http or https;
],
];
/**
* @var string $date_time_format 默认时间格式。
*/
private $dateTimeFormat = 'Y-m-d\TH:i:s\Z';
/**
* @var string $signName 签名。
*/
private $signName;
/**
* @var string $verifyPhoneTemplateCode 短信验证码对应的短信模板 ID 。
*/
private $verifyPhoneTemplateCode;
/**
* @var string $verifyPhoneTemplateField 短信模板变量中的验证码变量名。
*/
private $verifyPhoneTemplateField;
/**
* @var array $baseParams 公共请求参数。
*/
private $baseParams = [
'AccessKeyId' => null,
'Action' => null,
'Format' => 'json',
'RegionId' => 'cn-hangzhou',
'SignatureMethod' => 'HMAC-SHA1',
'SignatureNonce' => null,
'SignatureVersion' => '1.0',
'Timestamp' => null,
'Version' => '2017-05-25',
];
/**
* @var array $options 请求参数。
*/
private $options = [];
/**
* AliDysms constructor.
*
* @param string $access_key_id 访问密钥 ID 。
* @param string $access_key_secret 访问密钥。
*/
public function __construct($access_key_id = null, $access_key_secret = null)
{
// 在这里读取配置文件,初始化配置。
// $this->signName = '';
// $this->accessKeyId = '';
// $this->accessKeySecret = '';
// $this->verifyPhoneTemplateCode = '';
// $this->verifyPhoneTemplateField = '';
if ($access_key_id) {
$this->accessKeyId = $access_key_id;
}
if ($access_key_secret) {
$this->accessKeySecret = $access_key_secret;
}
}
/**
* 设置 API 的名称。
*
* @param string $action API 的名称。
*
* @return $this
*/
public function setAction($action)
{
$this->action = $action;
return $this;
}
/**
* 设置参数。
*
* @param string $key 参数名。
* @param mixed $value 参数值。
*
* @return $this
*/
public function setOption($key, $value)
{
$this->options[$key] = $value;
return $this;
}
/**
* 查看是否存在某参数。
*
* @param string $key 参数名。
*
* @return bool
*/
public function hasOption($key)
{
return isset($this->options[$key]);
}
/**
* 批量设置参数。
*
* @param array $options 参数数组。
* @param bool $cover 是否覆盖。
*
* @return $this
*/
public function setOptions($options, $cover = false)
{
if ($cover) {
foreach ($options as $k => $v) {
$this->options[$k] = $v;
}
} else {
$this->options = array_merge($this->options, $options);
}
return $this;
}
/**
* 执行请求。
*
* @return bool|mixed|string
* @throws \Exception
*/
public function execute()
{
$url = $this->endpoints['dysmsapi']['cn-hangzhou'];
$baseParams = $this->baseParams;
$baseParams['AccessKeyId'] = $this->accessKeyId;
$baseParams['Action'] = $this->action;
$baseParams['SignatureNonce'] = md5(uniqid(mt_rand(), true));
$baseParams['Timestamp'] = gmdate($this->dateTimeFormat);
// 如果请求参数中包含有公共参数中的字段,则保留请求参数中的字段。
$options = array_merge($this->options, $baseParams);
unset($options['Signature']);
$options['Signature'] = $this->computeSignature($options);
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->parsePostHttpBody($options));
curl_setopt($ch, CURLOPT_TIMEOUT, 80);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$http_headers = [
'Date:' . gmdate($this->dateTimeFormat),
'Accept:application/json',
'x-acs-signature-method:HMAC-SHA1',
'x-acs-signature-version:1.0',
'x-acs-region-id:cn-hangzhou',
'x-sdk-client:php/2.0.0',
'Content-MD5:' . base64_encode(md5(json_encode($options), true)),
// 'Content-Type:application/octet-stream;charset=utf-8',
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $http_headers);
$res = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$errno = curl_errno($ch);
$error = curl_error($ch);
curl_close($ch);
if ($errno > 0) {
throw new \Exception($error, $errno);
}
$res = json_decode($res, true);
if (200 != $status || 'OK' != $res['Code']) {
throw new \Exception('ERR[' . $res['Code'] . ']: ' . $res['Message']);
}
// 重置请求参数,保证后续再次使用不会遗留前次的参数。
$this->options = [];
return $res;
}
/**
* 发送短信。
*
* @param array|string $phone_numbers 手机号码,支持多个号码,多个号码字符串以英文半角逗号( , )隔开,支持数组。
* @param string $template_code 短信模板 ID 。
* @param string/array|null $templateParam 短信模板变量对应的实际值,支持 json 字符串,如果传入数组,则进行 json 编码。
* @param string $sign_name 短信签名名称。
* @param null $out_id 外部流水扩展字段。
*
* @return bool|mixed|string
* @throws \Exception
*/
public function send($phone_numbers, $template_code, $template_param = null, $sign_name = null, $out_id = null)
{
$this->setAction('SendSms');
if (is_array($phone_numbers)) {
$phone_numbers = join(',', $phone_numbers);
}
$this->setOptions([
'PhoneNumbers' => $phone_numbers,
'TemplateCode' => $template_code,
]);
if ($sign_name) {
$this->setOption('SignName', $sign_name);
}
if (!$this->hasOption('SignName')) {
$this->setOption('SignName', $this->signName);
}
if (is_string($template_param)) {
$this->setOption('TemplateParam', $template_param);
} elseif (is_array($template_param)) {
$this->setOption('TemplateParam', json_encode($template_param, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
}
if (is_string($out_id)) {
$this->setOption('OutId', $out_id);
}
return $this->execute();
}
/**
* 发送短信验证码。
*
* @param string $phone_number 手机号码。
* @param int $digit 短信验证码位数,默认 6 位。
* @param string $verify_code 验证码。
* @param string $template_code 短信模板 id 。
* @param string $field 验证码参数字段。
*
* @return bool|mixed|string
* @throws \Exception
*/
public function sendVerifyCode($phone_number, $digit = 6, $verify_code = null, $template_code = null, $field = null)
{
// 生成指定位数的短信验证码。
if (empty($verify_code)) {
$verify_code = '';
for ($i = 0; $i < intval($digit); $i++) $verify_code .= mt_rand(0, 9);
}
$template_code = empty($template_code) ? $this->verifyPhoneTemplateCode : $template_code;
$field = empty($field) ? $this->verifyPhoneTemplateField : $field;
$response = $this->send(
$phone_number,
$template_code,
[$field => $verify_code]
);
if ($response['code'] === 'OK' && $response['message'] === 'OK') {
$response['phone_number'] = $phone_number;
$response['verify_code'] = $verify_code;
return $response;
}
return false;
}
/**
* 查询短信发送记录和发送状态。
*
* @param string $phone_number 手机号码。
* @param string $send_date 日期格式,格式为 yyyyMMdd ,例如 20181225 ,可查询最近 30 天内的记录。
* @param int $current_page 当前页码。
* @param int $page_size 每页记录数量,取值范围为 1~50 。
* @param null|string $biz_id 发送回执 ID 。
*
* @return bool|mixed|string
* @throws \Exception
*/
public function getDetails($phone_number, $send_date, $current_page = 1, $page_size = 10, $biz_id = null)
{
$this->setAction('QuerySendDetails');
$this->setOptions([
'CurrentPage' => $current_page,
'PageSize' => $page_size,
'PhoneNumber' => $phone_number,
'SendDate' => $send_date,
]);
if ($biz_id) {
$this->setOption('BizId', $biz_id);
}
return $this->execute();
}
/**
* 编辑短信签名。
*
* @param string $sign_name 签名名称。
* @param int $sign_source 签名来源。
* @param string $remark 短信签名申请说明。
* @param array $sign_file_list 签名的证明文件。
*
* @return bool|mixed|string
* @throws \Exception
*/
private function editSign($sign_name, $sign_source, $remark, $sign_file_list = [])
{
$this->setOptions([
'SignName' => $sign_name,
'SignSource' => $sign_source,
'Remark' => $remark,
]);
if (is_array($sign_file_list)) {
$sign_file_list = array_values($sign_file_list);
foreach ($sign_file_list as $idx => $sign_file) {
$this->setOption('SignFileList.' . ($idx + 1) . '.FileSuffix', $sign_file['file_suffix']);
$this->setOption('SignFileList.' . ($idx + 1) . '.FileContents', $sign_file['file_contents']);
}
}
return $this->execute();
}
/**
* 申请短信签名。
*
* @param string $sign_name 签名名称。
* @param int $sign_source 签名来源。其中:
*
* 0:企事业单位的全称或简称。
* 1:工信部备案网站的全称或简称。
* 2:APP 应用的全称或简称。
* 3:公众号或小程序的全称或简称。
* 4:电商平台店铺名的全称或简称。
* 5:商标名的全称或简称
*
* @param string $remark 短信签名申请说明。
* @param array $sign_file_list 签名的证明文件。以数组形式传入,格式如下:
*
* $sign_file_list = [
* ['file_suffix' => 'jpg','file_contents' => 'R0lGOD...iwAA'],
* ['file_suffix' => 'jpg','file_contents' => 'R0lGOD...iwAA'],
* ['file_suffix' => 'jpg','file_contents' => 'R0lGOD...iwAA'],
* ['file_suffix' => 'jpg','file_contents' => 'R0lGOD...iwAA'],
* ];
*
* @return bool|mixed|string
* @throws \Exception
*/
public function addSign($sign_name, $sign_source, $remark, $sign_file_list = [])
{
$this->setAction('AddSmsSign');
return $this->editSign($sign_name, $sign_source, $remark, $sign_file_list);
}
/**
* 删除短信签名。
*
* @param string $sign_name 短信签名。
*
* @return bool|mixed|string
* @throws \Exception
*/
public function deleteSign($sign_name)
{
$this->setAction('DeleteSmsSign');
$this->setOption('SignName', $sign_name);
return $this->execute();
}
/**
* 修改未审核通过的短信签名,并重新提交审核。
*
* 参数格式参考 addSign 方法。
*
* @param string $sign_name 签名名称。
* @param int $sign_source 签名来源。
* @param string $remark 短信签名申请说明。
* @param array $sign_file_list 签名的证明文件。
*
* @return bool|mixed|string
* @throws Exception
*/
public function modifySign($sign_name, $sign_source, $remark, $sign_file_list = [])
{
$this->setAction('ModifySmsSign');
return $this->editSign($sign_name, $sign_source, $remark, $sign_file_list);
}
/**
* 查询短信签名申请状态。
*
* @param string $sign_name 短信签名名称。
*
* @return bool|mixed|string
* @throws \Exception
*/
public function getSign($sign_name)
{
$this->setAction('QuerySmsSign');
$this->setOption('SignName', $sign_name);
return $this->execute();
}
/**
* 编辑短信模板。
*
* @param string $template_name 模板名称。
* @param int $template_type 短信类型。
* @param string $template_content 模板内容。
* @param string $remark 短信模板申请说明。
* @param string $template_code 短信模板 CODE 。
*
* @return bool|mixed|string
* @throws \Exception
*/
private function editTemplate($template_name, $template_type, $template_content, $remark, $template_code = null)
{
$this->setOptions([
'TemplateName' => $template_name,
'TemplateType' => intval($template_type),
'TemplateContent' => $template_content,
'Remark' => $remark,
]);
if ($template_code) {
$this->setOption('TemplateCode', $template_code);
}
return $this->execute();
}
/**
* 申请短信模板。
*
* @param string $template_name 模板名称。
* @param int $template_type 短信类型。其中:
*
* 0:验证码。
* 1:短信通知。
* 2:推广短信。
* 3:国际/港澳台消息。
*
* @param string $template_content 模板内容,长度为 1~500 个字符。
* @param string $remark 短信模板申请说明。
*
* @return bool|mixed|string
* @throws \Exception
*/
public function addTemplate($template_name, $template_type, $template_content, $remark)
{
$this->setAction('AddSmsTemplate');
return $this->editTemplate($template_name, $template_type, $template_content, $remark);
}
/**
* 删除短信模板。
*
* @param string $template_code 短信模板 CODE 。
*
* @return bool|mixed|string
* @throws Exception
*/
public function deleteTemplate($template_code)
{
$this->setAction('DeleteSmsTemplate');
$this->setOption('TemplateCode', $template_code);
return $this->execute();
}
/**
* 修改未通过审核的短信模板。
*
* 参数格式参考 addTemplate 方法。
*
* @param string $template_code 短信模板 CODE 。
* @param string $template_name 模板名称。
* @param int $template_type 短信类型。
* @param string $template_content 模板内容,长度为 1~500 个字符。
* @param string $remark 短信模板申请说明。
*
* @return bool|mixed|string
* @throws \Exception
*/
public function modifyTemplate($template_code, $template_name, $template_type, $template_content, $remark)
{
$this->setAction('ModifySmsTemplate');
return $this->editTemplate($template_name, $template_type, $template_content, $remark, $template_code);
}
/**
* 查询短信模板的审核状态。
*
* @param string $template_code 短信模板 CODE 。
*
* @return bool|mixed|string
* @throws \Exception
*/
public function getTemplate($template_code)
{
$this->setAction('QuerySmsTemplate');
$this->setOption('TemplateCode', $template_code);
return $this->execute();
}
/**
* 根据 POP 规则对要签名的字符串进行编码。
*
* @param string $str 要编码的字符串。
*
* @return string|string[]|null 已编码的字符串。
*/
private function percentEncode($str)
{
$res = urlencode($str);
$res = preg_replace('/\+/', '%20', $res);
$res = preg_replace('/\*/', '%2A', $res);
$res = preg_replace('/%7E/', '~', $res);
return $res;
}
/**
* 计算签名。
*
* @param array $params 请求参数。
*
* @return string 签名。
*/
private function computeSignature($params)
{
global $access_secret;
ksort($params);
$sourceArr = [];
foreach ($params as $k => $v) {
$sourceArr[] = $this->percentEncode($k) . '=' . $this->percentEncode($v);
}
$source = join('&', $sourceArr);
$source = 'POST' . '&' . $this->percentEncode('/') . '&' . $this->percentEncode($source);
return base64_encode(hash_hmac('sha1', $source, $this->accessKeySecret . '&', true));
}
/**
* 拼接请求体。
*
* @param array $params 请求参数。
*
* @return string 请求体字符串。
*/
private function parsePostHttpBody($params)
{
$bodyArr = [];
foreach ($params as $k => $v) {
$bodyArr[] = $k . '=' . urlencode($v);
}
return join('&', $bodyArr);
}
}