forked from daixianceng/yii2-smser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
YuntongxunSmser.php
153 lines (131 loc) · 4.02 KB
/
YuntongxunSmser.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
<?php
namespace daixianceng\smser;
use yii\base\InvalidConfigException;
use yii\base\NotSupportedException;
/**
* 云通讯
*
* @author Cosmo <daixianceng@gmail.com>
* @property string $state read-only state
* @property string $message read-only message
*/
class YuntongxunSmser extends Smser
{
/**
* @var string
*/
public $accountSid;
/**
* @var string
*/
public $accountToken;
/**
* @var string
*/
public $appId;
/**
* @var string
*/
public $serverIp;
/**
* @var string
*/
public $serverPort;
/**
* @var string
*/
public $softVersion;
/**
* @var string
*/
public $dataType = 'json';
/**
* @var string|null
*/
private $_batch;
/**
* @inheritdoc
*/
public function send($mobile, $content)
{
throw new NotSupportedException('云通讯不支持直接发送文本!');
}
/**
* @inheritdoc
*/
public function sendByTemplate($mobile, $data, $id)
{
if (parent::sendByTemplate($mobile, $data, $id)) {
return true;
}
if ($this->dataType === 'json') {
$body = json_encode([
'to' => $mobile,
'templateId' => $id,
'appId' => $this->appId,
'datas' => array_values($data)
]);
} elseif ($this->dataType === 'xml') {
$dataStr = '';
foreach ($data as $val) {
$dataStr .= "<data>{$val}</data>";
}
$body = <<<XML
<TemplateSMS>
<to>{$mobile}</to>
<appId>{$this->appId}</appId>
<templateId>{$id}</templateId>
<datas>{$dataStr}</datas>
</TemplateSMS>
XML;
} else {
throw new InvalidConfigException('“dataType” 配置不正确。');
}
$sig = strtoupper(md5($this->accountSid . $this->accountToken . $this->getBatch()));
$this->url = "https://{$this->serverIp}:{$this->serverPort}/{$this->softVersion}/Accounts/{$this->accountSid}/SMS/TemplateSMS?sig={$sig}";
$authen = base64_encode($this->accountSid . ':' . $this->getBatch());
$header = ["Accept:application/{$this->dataType}", "Content-Type:application/{$this->dataType};charset=utf-8", "Authorization:{$authen}"];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$result = curl_exec($ch);
curl_close($ch);
if (empty($result)) {
$this->state = '172001';
$this->message = '网络错误';
} else {
if ($this->dataType === 'json') {
$json = json_decode($result);
if ($json && is_object($json)) {
$this->state = isset($json->statusCode) ? (string) $json->statusCode : null;
$this->message = isset($json->statusMsg) ? (string) $json->statusMsg : null;
}
} else {
$xml = simplexml_load_string(trim($result, " \t\n\r"));
if ($xml && is_object($xml)) {
$this->state = isset($xml->statusCode) ? (string) $xml->statusCode : null;
$this->message = isset($xml->statusMsg) ? (string) $xml->statusMsg : null;
}
}
}
return $this->state === '000000';
}
/**
* 时间戳
*
* @return string
*/
public function getBatch()
{
if ($this->_batch === null) {
$this->_batch = date('YmdHis');
}
return $this->_batch;
}
}