|
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) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->Batch = date('YmdHis'); |
|
30
|
|
|
$this->ServerIP = $ServerIP; |
|
31
|
|
|
$this->ServerPort = $ServerPort; |
|
32
|
|
|
$this->SoftVersion = $SoftVersion; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* 设置主帐号 |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $AccountSid 主帐号 |
|
39
|
|
|
* @param string $AccountToken 主帐号Token |
|
40
|
|
|
*/ |
|
41
|
|
|
public function setAccount($AccountSid, $AccountToken) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->AccountSid = $AccountSid; |
|
44
|
|
|
$this->AccountToken = $AccountToken; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* 设置应用ID |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $AppId 应用ID |
|
51
|
|
|
*/ |
|
52
|
|
|
public function setAppId($AppId) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->AppId = $AppId; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* 发起HTTPS请求 |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $url |
|
61
|
|
|
* @param mixed $data |
|
62
|
|
|
* @param mixed $header |
|
63
|
|
|
* @param mixed $post |
|
64
|
|
|
* |
|
65
|
|
|
* @return mixed |
|
66
|
|
|
*/ |
|
67
|
|
|
public function curl_post($url, $data, $header, $post = 1) |
|
68
|
|
|
{ |
|
69
|
|
|
//初始化curl |
|
70
|
|
|
$ch = curl_init(); |
|
71
|
|
|
//参数设置 |
|
72
|
|
|
$res = curl_setopt($ch, CURLOPT_URL, $url); |
|
|
|
|
|
|
73
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
|
74
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
|
75
|
|
|
curl_setopt($ch, CURLOPT_HEADER, 0); |
|
76
|
|
|
curl_setopt($ch, CURLOPT_POST, $post); |
|
77
|
|
|
if ($post) { |
|
78
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); |
|
79
|
|
|
} |
|
80
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
81
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); |
|
82
|
|
|
$result = curl_exec($ch); |
|
83
|
|
|
//连接失败 |
|
84
|
|
|
if ($result === false) { |
|
85
|
|
|
if ($this->BodyType === 'json') { |
|
86
|
|
|
$result = '{"statusCode":"172001","statusMsg":"网络错误"}'; |
|
87
|
|
|
} else { |
|
88
|
|
|
$result = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Response><statusCode>172001</statusCode><statusMsg>网络错误</statusMsg></Response>'; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
curl_close($ch); |
|
92
|
|
|
|
|
93
|
|
|
return $result; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* 发送模板短信 |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $to |
|
100
|
|
|
* 短信接收彿手机号码集合,用英文逗号分开 |
|
101
|
|
|
* @param array $datas |
|
102
|
|
|
* 内容数据 |
|
103
|
|
|
* @param mixed $tempId |
|
104
|
|
|
* 模板Id |
|
105
|
|
|
* |
|
106
|
|
|
* @return mixed |
|
107
|
|
|
*/ |
|
108
|
|
|
public function sendTemplateSMS($to, $datas, $tempId) |
|
109
|
|
|
{ |
|
110
|
|
|
//主帐号鉴权信息验证,对必选参数进行判空。 |
|
111
|
|
|
$auth = $this->accAuth(); |
|
112
|
|
|
if ($auth !== true) { |
|
113
|
|
|
return $auth; |
|
114
|
|
|
} |
|
115
|
|
|
// 拼接请求包体 |
|
116
|
|
|
if ($this->BodyType === 'json') { |
|
117
|
|
|
$data = ''; |
|
118
|
|
View Code Duplication |
for ($i = 0; $i < count($datas); $i++) { |
|
|
|
|
|
|
119
|
|
|
$data = $data . "'" . $datas[$i] . "',"; |
|
120
|
|
|
} |
|
121
|
|
|
$body = "{'to':'$to','templateId':'$tempId','appId':'$this->AppId','datas':[" . $data . ']}'; |
|
122
|
|
|
} else { |
|
123
|
|
|
$data = ''; |
|
124
|
|
View Code Duplication |
for ($i = 0; $i < count($datas); $i++) { |
|
|
|
|
|
|
125
|
|
|
$data = $data . '<data>' . $datas[$i] . '</data>'; |
|
126
|
|
|
} |
|
127
|
|
|
$body = "<TemplateSMS> |
|
128
|
|
|
<to>$to</to> |
|
129
|
|
|
<appId>$this->AppId</appId> |
|
130
|
|
|
<templateId>$tempId</templateId> |
|
131
|
|
|
<datas>" . $data . '</datas> |
|
132
|
|
|
</TemplateSMS>'; |
|
133
|
|
|
} |
|
134
|
|
|
// 大写的sig参数 |
|
135
|
|
|
$sig = strtoupper(md5($this->AccountSid . $this->AccountToken . $this->Batch)); |
|
136
|
|
|
// 生成请求URL |
|
137
|
|
|
$url = "https://$this->ServerIP:$this->ServerPort/$this->SoftVersion/Accounts/$this->AccountSid/SMS/TemplateSMS?sig=$sig"; |
|
138
|
|
|
// 生成授权:主帐户Id + 英文冒号 + 时间戳。 |
|
139
|
|
|
$authen = base64_encode($this->AccountSid . ':' . $this->Batch); |
|
140
|
|
|
// 生成包头 |
|
141
|
|
|
$header = array("Accept:application/$this->BodyType", "Content-Type:application/$this->BodyType;charset=utf-8", "Authorization:$authen"); |
|
142
|
|
|
// 发送请求 |
|
143
|
|
|
$result = $this->curl_post($url, $body, $header); |
|
144
|
|
View Code Duplication |
if ($this->BodyType === 'json') {//JSON格式 |
|
|
|
|
|
|
145
|
|
|
$datas = json_decode($result); |
|
146
|
|
|
} else { //xml格式 |
|
147
|
|
|
$datas = simplexml_load_string(trim($result, " \t\n\r")); |
|
148
|
|
|
} |
|
149
|
|
|
//重新装填数据 |
|
150
|
|
|
if ($datas->statusCode === 0) { |
|
151
|
|
|
if ($this->BodyType === 'json') { |
|
152
|
|
|
$datas->TemplateSMS = $datas->templateSMS; |
|
153
|
|
|
unset($datas->templateSMS); |
|
154
|
|
|
} |
|
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, $respUrl, $lang, $userData, $welcomePrompt, $playVerifyCode) |
|
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
|
|
|
|