1
|
|
|
|
|
|
|
|
2
|
|
|
<?php |
3
|
|
|
/* |
4
|
|
|
* Copyright (c) 2014 The CCP project authors. All Rights Reserved. |
5
|
|
|
* |
6
|
|
|
* Use of this source code is governed by a Beijing Speedtong Information Technology Co.,Ltd license |
7
|
|
|
* that can be found in the LICENSE file in the root of the web site. |
8
|
|
|
* |
9
|
|
|
* http://www.yuntongxun.com |
10
|
|
|
* |
11
|
|
|
* An additional intellectual property rights grant can be found |
12
|
|
|
* in the file PATENTS. All contributing project authors may |
13
|
|
|
* be found in the AUTHORS file in the root of the source tree. |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
class REST |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
private $AccountSid; |
19
|
|
|
private $AccountToken; |
20
|
|
|
private $AppId; |
21
|
|
|
private $ServerIP; |
22
|
|
|
private $ServerPort; |
23
|
|
|
private $SoftVersion; |
24
|
|
|
private $Batch; //时间戳 |
25
|
|
|
private $BodyType = 'xml'; //包体格式,可填值:json 、xml |
26
|
|
|
|
27
|
|
|
public function __construct($ServerIP, $ServerPort, $SoftVersion, $BodyType = 'xml') |
28
|
|
|
{ |
29
|
|
|
$this->Batch = date('YmdHis'); |
30
|
|
|
$this->ServerIP = $ServerIP; |
31
|
|
|
$this->ServerPort = $ServerPort; |
32
|
|
|
$this->SoftVersion = $SoftVersion; |
33
|
|
|
if (in_array($BodyType, ['xml', 'json'])) { |
34
|
|
|
$this->BodyType = $BodyType; |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* 设置主帐号 |
40
|
|
|
* |
41
|
|
|
* @param string $AccountSid 主帐号 |
42
|
|
|
* @param string $AccountToken 主帐号Token |
43
|
|
|
*/ |
44
|
|
|
public function setAccount($AccountSid, $AccountToken) |
45
|
|
|
{ |
46
|
|
|
$this->AccountSid = $AccountSid; |
47
|
|
|
$this->AccountToken = $AccountToken; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* 设置应用ID |
52
|
|
|
* |
53
|
|
|
* @param string $AppId 应用ID |
54
|
|
|
*/ |
55
|
|
|
public function setAppId($AppId) |
56
|
|
|
{ |
57
|
|
|
$this->AppId = $AppId; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* 发起HTTPS请求 |
62
|
|
|
* |
63
|
|
|
* @param string $url |
64
|
|
|
* @param mixed $data |
65
|
|
|
* @param mixed $header |
66
|
|
|
* @param mixed $post |
67
|
|
|
* |
68
|
|
|
* @return mixed |
69
|
|
|
*/ |
70
|
|
|
public function curl_post($url, $data, $header, $post = 1) |
71
|
|
|
{ |
72
|
|
|
//初始化curl |
73
|
|
|
$ch = curl_init(); |
74
|
|
|
//参数设置 |
75
|
|
|
$res = curl_setopt($ch, CURLOPT_URL, $url); |
|
|
|
|
76
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
77
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
78
|
|
|
curl_setopt($ch, CURLOPT_HEADER, 0); |
79
|
|
|
curl_setopt($ch, CURLOPT_POST, $post); |
80
|
|
|
if ($post) { |
81
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); |
82
|
|
|
} |
83
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
84
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); |
85
|
|
|
$result = curl_exec($ch); |
86
|
|
|
//连接失败 |
87
|
|
|
if ($result === false) { |
88
|
|
|
if ($this->BodyType === 'json') { |
89
|
|
|
$result = '{"statusCode":"172001","statusMsg":"网络错误"}'; |
90
|
|
|
} else { |
91
|
|
|
$result = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Response><statusCode>172001</statusCode><statusMsg>网络错误</statusMsg></Response>'; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
curl_close($ch); |
95
|
|
|
|
96
|
|
|
return $result; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* 发送模板短信 |
101
|
|
|
* |
102
|
|
|
* @param string $to |
103
|
|
|
* 短信接收彿手机号码集合,用英文逗号分开 |
104
|
|
|
* @param array $datas |
105
|
|
|
* 内容数据 |
106
|
|
|
* @param mixed $tempId |
107
|
|
|
* 模板Id |
108
|
|
|
* |
109
|
|
|
* @return mixed |
110
|
|
|
*/ |
111
|
|
|
public function sendTemplateSMS($to, $datas, $tempId) |
112
|
|
|
{ |
113
|
|
|
//主帐号鉴权信息验证,对必选参数进行判空。 |
114
|
|
|
$auth = $this->accAuth(); |
115
|
|
|
if ($auth !== true) { |
116
|
|
|
return $auth; |
117
|
|
|
} |
118
|
|
|
// 拼接请求包体 |
119
|
|
|
if ($this->BodyType === 'json') { |
120
|
|
|
$data = ''; |
121
|
|
View Code Duplication |
for ($i = 0; $i < count($datas); $i++) { |
|
|
|
|
122
|
|
|
$data = $data . "'" . $datas[$i] . "',"; |
123
|
|
|
} |
124
|
|
|
$body = "{'to':'$to','templateId':'$tempId','appId':'$this->AppId','datas':[" . $data . ']}'; |
125
|
|
|
} else { |
126
|
|
|
$data = ''; |
127
|
|
View Code Duplication |
for ($i = 0; $i < count($datas); $i++) { |
|
|
|
|
128
|
|
|
$data = $data . '<data>' . $datas[$i] . '</data>'; |
129
|
|
|
} |
130
|
|
|
$body = "<TemplateSMS> |
131
|
|
|
<to>$to</to> |
132
|
|
|
<appId>$this->AppId</appId> |
133
|
|
|
<templateId>$tempId</templateId> |
134
|
|
|
<datas>" . $data . '</datas> |
135
|
|
|
</TemplateSMS>'; |
136
|
|
|
} |
137
|
|
|
// 大写的sig参数 |
138
|
|
|
$sig = strtoupper(md5($this->AccountSid . $this->AccountToken . $this->Batch)); |
139
|
|
|
// 生成请求URL |
140
|
|
|
$url = "https://$this->ServerIP:$this->ServerPort/$this->SoftVersion/Accounts/$this->AccountSid/SMS/TemplateSMS?sig=$sig"; |
141
|
|
|
// 生成授权:主帐户Id + 英文冒号 + 时间戳。 |
142
|
|
|
$authen = base64_encode($this->AccountSid . ':' . $this->Batch); |
143
|
|
|
// 生成包头 |
144
|
|
|
$header = array("Accept:application/$this->BodyType", "Content-Type:application/$this->BodyType;charset=utf-8", "Authorization:$authen"); |
145
|
|
|
// 发送请求 |
146
|
|
|
$result = $this->curl_post($url, $body, $header); |
147
|
|
View Code Duplication |
if ($this->BodyType === 'json') {//JSON格式 |
|
|
|
|
148
|
|
|
$datas = json_decode($result); |
149
|
|
|
} else { //xml格式 |
150
|
|
|
$datas = simplexml_load_string(trim($result, " \t\n\r")); |
151
|
|
|
} |
152
|
|
|
// 重新装填数据 |
153
|
|
|
if (isset($datas->templateSMS)) { |
154
|
|
|
$datas->TemplateSMS = $datas->templateSMS; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $datas; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* 语音验证码 |
162
|
|
|
* |
163
|
|
|
* @param mixed $verifyCode 验证码内容,为数字和英文字母,不区分大小写,长度4-8位 |
164
|
|
|
* @param mixed $playTimes 播放次数,1-3次 |
165
|
|
|
* @param mixed $to 接收号码 |
166
|
|
|
* @param mixed $displayNum 显示的主叫号码 |
167
|
|
|
* @param mixed $respUrl 语音验证码状态通知回调地址,云通讯平台将向该Url地址发送呼叫结果通知 |
168
|
|
|
* @param mixed $lang 语言类型 |
169
|
|
|
* @param mixed $userData 第三方私有数据 |
170
|
|
|
* @param mixed $welcomePrompt 欢迎提示音,在播放验证码语音前播放此内容(语音文件格式为wav) |
171
|
|
|
* @param mixed $playVerifyCode 语音验证码的内容全部播放此节点下的全部语音文件 |
172
|
|
|
* |
173
|
|
|
* @return mixed |
174
|
|
|
*/ |
175
|
|
|
public function voiceVerify($verifyCode, $playTimes, $to, $displayNum = null, $respUrl = null, $lang = 'zh', $userData = null, $welcomePrompt = null, $playVerifyCode = null) |
176
|
|
|
{ |
177
|
|
|
//主帐号鉴权信息验证,对必选参数进行判空。 |
178
|
|
|
$auth = $this->accAuth(); |
179
|
|
|
if ($auth !== true) { |
180
|
|
|
return $auth; |
181
|
|
|
} |
182
|
|
|
// 拼接请求包体 |
183
|
|
|
if ($this->BodyType === 'json') { |
184
|
|
|
$body = "{'appId':'$this->AppId','verifyCode':'$verifyCode','playTimes':'$playTimes','to':'$to','respUrl':'$respUrl','displayNum':'$displayNum', |
185
|
|
|
'lang':'$lang','userData':'$userData','welcomePrompt':'$welcomePrompt','playVerifyCode':'$playVerifyCode'}"; |
186
|
|
|
} else { |
187
|
|
|
$body = "<VoiceVerify> |
188
|
|
|
<appId>$this->AppId</appId> |
189
|
|
|
<verifyCode>$verifyCode</verifyCode> |
190
|
|
|
<playTimes>$playTimes</playTimes> |
191
|
|
|
<to>$to</to> |
192
|
|
|
<respUrl>$respUrl</respUrl> |
193
|
|
|
<displayNum>$displayNum</displayNum> |
194
|
|
|
<lang>$lang</lang> |
195
|
|
|
<userData>$userData</userData> |
196
|
|
|
<welcomePrompt>$welcomePrompt</welcomePrompt> |
197
|
|
|
<playVerifyCode>$playVerifyCode</playVerifyCode> |
198
|
|
|
</VoiceVerify>"; |
199
|
|
|
} |
200
|
|
|
// 大写的sig参数 |
201
|
|
|
$sig = strtoupper(md5($this->AccountSid . $this->AccountToken . $this->Batch)); |
202
|
|
|
// 生成请求URL |
203
|
|
|
$url = "https://$this->ServerIP:$this->ServerPort/$this->SoftVersion/Accounts/$this->AccountSid/Calls/VoiceVerify?sig=$sig"; |
204
|
|
|
// 生成授权:主帐户Id + 英文冒号 + 时间戳。 |
205
|
|
|
$authen = base64_encode($this->AccountSid . ':' . $this->Batch); |
206
|
|
|
// 生成包头 |
207
|
|
|
$header = array("Accept:application/$this->BodyType", "Content-Type:application/$this->BodyType;charset=utf-8", "Authorization:$authen"); |
208
|
|
|
// 发送请求 |
209
|
|
|
$result = $this->curl_post($url, $body, $header); |
210
|
|
View Code Duplication |
if ($this->BodyType === 'json') {//JSON格式 |
|
|
|
|
211
|
|
|
$datas = json_decode($result); |
212
|
|
|
} else { //xml格式 |
213
|
|
|
$datas = simplexml_load_string(trim($result, " \t\n\r")); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
return $datas; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* 主帐号鉴权 |
221
|
|
|
* |
222
|
|
|
* @return mixed |
223
|
|
|
*/ |
224
|
|
|
public function accAuth() |
225
|
|
|
{ |
226
|
|
|
if ($this->ServerIP === '') { |
227
|
|
|
$data = new stdClass(); |
228
|
|
|
$data->statusCode = '172004'; |
229
|
|
|
$data->statusMsg = 'IP为空'; |
230
|
|
|
|
231
|
|
|
return $data; |
232
|
|
|
} |
233
|
|
|
if ($this->ServerPort <= 0) { |
234
|
|
|
$data = new stdClass(); |
235
|
|
|
$data->statusCode = '172005'; |
236
|
|
|
$data->statusMsg = '端口错误(小于等于0)'; |
237
|
|
|
|
238
|
|
|
return $data; |
239
|
|
|
} |
240
|
|
|
if ($this->SoftVersion === '') { |
241
|
|
|
$data = new stdClass(); |
242
|
|
|
$data->statusCode = '172013'; |
243
|
|
|
$data->statusMsg = '版本号为空'; |
244
|
|
|
|
245
|
|
|
return $data; |
246
|
|
|
} |
247
|
|
|
if ($this->AccountSid === '') { |
248
|
|
|
$data = new stdClass(); |
249
|
|
|
$data->statusCode = '172006'; |
250
|
|
|
$data->statusMsg = '主帐号为空'; |
251
|
|
|
|
252
|
|
|
return $data; |
253
|
|
|
} |
254
|
|
|
if ($this->AccountToken === '') { |
255
|
|
|
$data = new stdClass(); |
256
|
|
|
$data->statusCode = '172007'; |
257
|
|
|
$data->statusMsg = '主帐号令牌为空'; |
258
|
|
|
|
259
|
|
|
return $data; |
260
|
|
|
} |
261
|
|
|
if ($this->AppId === '') { |
262
|
|
|
$data = new stdClass(); |
263
|
|
|
$data->statusCode = '172012'; |
264
|
|
|
$data->statusMsg = '应用ID为空'; |
265
|
|
|
|
266
|
|
|
return $data; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
return true; |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|