1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Toplan\PhpSms; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class AlidayuAgent |
7
|
|
|
* |
8
|
|
|
* @property string $sendUrl |
9
|
|
|
* @property string $appKey |
10
|
|
|
* @property string $secretKey |
11
|
|
|
* @property string $smsFreeSignName |
12
|
|
|
* @property string $calledShowNum |
13
|
|
|
*/ |
14
|
|
|
class AlidayuAgent extends Agent implements TemplateSms |
15
|
|
|
{ |
16
|
|
|
public function sendSms($to, $content, $tempId, array $data) |
17
|
|
|
{ |
18
|
|
|
$this->sendTemplateSms($to, $tempId, $data); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function sendTemplateSms($to, $tempId, array $data) |
22
|
|
|
{ |
23
|
|
|
$params = [ |
24
|
|
|
'method' => 'alibaba.aliqin.fc.sms.num.send', |
25
|
|
|
'sms_type' => 'normal', |
26
|
|
|
'sms_free_sign_name' => $this->smsFreeSignName, |
27
|
|
|
'sms_param' => $this->getTempDataString($data), |
28
|
|
|
'rec_num' => $to, |
29
|
|
|
'sms_template_code' => $tempId, |
30
|
|
|
]; |
31
|
|
|
$this->request($params); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function voiceVerify($to, $code, $tempId, array $data) |
35
|
|
|
{ |
36
|
|
|
$params = [ |
37
|
|
|
'called_num' => $to, |
38
|
|
|
'called_show_num' => $this->calledShowNum, |
39
|
|
|
]; |
40
|
|
|
if ($tempId) { |
41
|
|
|
//文本转语音通知 |
42
|
|
|
$params['method'] = 'alibaba.aliqin.fc.tts.num.singlecall'; |
43
|
|
|
$params['tts_code'] = $tempId; |
44
|
|
|
$params['tts_param'] = $this->getTempDataString($data); |
45
|
|
|
} elseif ($code) { |
46
|
|
|
//语音通知 |
47
|
|
|
$params['method'] = 'alibaba.aliqin.fc.voice.num.singlecall'; |
48
|
|
|
$params['voice_code'] = $code; |
49
|
|
|
} |
50
|
|
|
$this->request($params); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
protected function request(array $params) |
54
|
|
|
{ |
55
|
|
|
$params = $this->createParams($params); |
56
|
|
|
$result = $this->curl($this->sendUrl, $params, true); |
57
|
|
|
$this->setResult($result, $this->genResponseName($params['method'])); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
protected function createParams(array $params) |
61
|
|
|
{ |
62
|
|
|
$params = array_merge([ |
63
|
|
|
'app_key' => $this->appKey, |
64
|
|
|
'v' => '2.0', |
65
|
|
|
'format' => 'json', |
66
|
|
|
'sign_method' => 'md5', |
67
|
|
|
'timestamp' => date('Y-m-d H:i:s'), |
68
|
|
|
], $params); |
69
|
|
|
$params['sign'] = $this->genSign($params); |
70
|
|
|
|
71
|
|
|
return $params; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
protected function genSign($params) |
75
|
|
|
{ |
76
|
|
|
ksort($params); |
77
|
|
|
$stringToBeSigned = $this->secretKey; |
78
|
|
|
foreach ($params as $k => $v) { |
79
|
|
|
if (is_string($v) && '@' !== substr($v, 0, 1)) { |
80
|
|
|
$stringToBeSigned .= "$k$v"; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
unset($k, $v); |
84
|
|
|
$stringToBeSigned .= $this->secretKey; |
85
|
|
|
|
86
|
|
|
return strtoupper(md5($stringToBeSigned)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
protected function setResult($result, $callbackName) |
90
|
|
|
{ |
91
|
|
|
if ($result['request']) { |
92
|
|
|
$result = json_decode($result['response'], true); |
93
|
|
|
if (isset($result[$callbackName]['result'])) { |
94
|
|
|
$result = $result[$callbackName]['result']; |
95
|
|
|
$this->result(Agent::SUCCESS, (bool) $result['success']); |
96
|
|
|
$this->result(Agent::INFO, json_encode($result)); |
97
|
|
|
$this->result(Agent::CODE, $result['err_code']); |
98
|
|
|
} elseif (isset($result['error_response'])) { |
99
|
|
|
$error = $result['error_response']; |
100
|
|
|
$this->result(Agent::INFO, json_encode($error)); |
101
|
|
|
$this->result(Agent::CODE, $error['code']); |
102
|
|
|
} |
103
|
|
|
} else { |
104
|
|
|
$this->result(Agent::INFO, 'request failed'); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
protected function genResponseName($method) |
109
|
|
|
{ |
110
|
|
|
return str_replace('.', '_', $method) . '_response'; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
protected function getTempDataString(array $data) |
114
|
|
|
{ |
115
|
|
|
$data = array_map(function ($value) { |
116
|
|
|
return (string) $value; |
117
|
|
|
}, $data); |
118
|
|
|
|
119
|
|
|
return json_encode($data); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|