1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Created by PhpStorm. |
5
|
|
|
* User: UCPAAS JackZhao |
6
|
|
|
* Date: 2014/10/22 |
7
|
|
|
* Time: 12:04 |
8
|
|
|
* Dec : ucpass php demo |
9
|
|
|
*/ |
10
|
|
|
class Ucpaas |
|
|
|
|
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* 云之讯REST API版本号。当前版本号为:2014-06-30 |
14
|
|
|
*/ |
15
|
|
|
const SoftVersion = '2014-06-30'; |
16
|
|
|
/** |
17
|
|
|
* API请求地址 |
18
|
|
|
*/ |
19
|
|
|
const BaseUrl = 'https://api.ucpaas.com/'; |
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
* 开发者账号ID。由32个英文字母和阿拉伯数字组成的开发者账号唯一标识符。 |
23
|
|
|
*/ |
24
|
|
|
private $accountSid; |
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
* 开发者账号TOKEN |
28
|
|
|
*/ |
29
|
|
|
private $token; |
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
* 时间戳 |
33
|
|
|
*/ |
34
|
|
|
private $timestamp; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param mixed $options 数组参数必填 |
38
|
|
|
* $options = array( |
39
|
|
|
* |
40
|
|
|
* ) |
41
|
|
|
* |
42
|
|
|
* @throws Exception |
43
|
|
|
*/ |
44
|
|
|
public function __construct($options) |
45
|
|
|
{ |
46
|
|
|
date_default_timezone_set('Asia/Shanghai'); |
47
|
|
|
if (is_array($options) && !empty($options)) { |
48
|
|
|
$this->accountSid = isset($options['accountsid']) ? $options['accountsid'] : ''; |
49
|
|
|
$this->token = isset($options['token']) ? $options['token'] : ''; |
50
|
|
|
$this->timestamp = date('YmdHis'); |
51
|
|
|
} else { |
52
|
|
|
throw new Exception('非法参数'); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return string |
58
|
|
|
* 包头验证信息,使用Base64编码(账户Id:时间戳) |
59
|
|
|
*/ |
60
|
|
|
private function getAuthorization() |
61
|
|
|
{ |
62
|
|
|
$data = $this->accountSid . ':' . $this->timestamp; |
63
|
|
|
|
64
|
|
|
return trim(base64_encode($data)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return string |
69
|
|
|
* 验证参数,URL后必须带有sig参数,sig= MD5(账户Id + 账户授权令牌 + 时间戳,共32位)(注:转成大写) |
70
|
|
|
*/ |
71
|
|
|
private function getSigParameter() |
72
|
|
|
{ |
73
|
|
|
$sig = $this->accountSid . $this->token . $this->timestamp; |
74
|
|
|
|
75
|
|
|
return strtoupper(md5($sig)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $url |
80
|
|
|
* @param string|null $body |
81
|
|
|
* @param string $type |
82
|
|
|
* @param string $method |
83
|
|
|
* |
84
|
|
|
* @return mixed|string |
85
|
|
|
*/ |
86
|
|
|
private function getResult($url, $body, $type, $method) |
87
|
|
|
{ |
88
|
|
|
$data = $this->connection($url, $body, $type, $method); |
89
|
|
|
if (isset($data) && !empty($data)) { |
90
|
|
|
$result = $data; |
91
|
|
|
} else { |
92
|
|
|
$result = '没有返回数据'; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $result; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $url |
100
|
|
|
* @param string $type |
101
|
|
|
* @param string|null $body |
102
|
|
|
* @param string $method |
103
|
|
|
* |
104
|
|
|
* @return mixed|string |
105
|
|
|
*/ |
106
|
|
|
private function connection($url, $body, $type, $method) |
107
|
|
|
{ |
108
|
|
|
if ($type === 'json') { |
109
|
|
|
$mine = 'application/json'; |
110
|
|
|
} else { |
111
|
|
|
$mine = 'application/xml'; |
112
|
|
|
} |
113
|
|
|
if (function_exists('curl_init')) { |
114
|
|
|
$header = array( |
115
|
|
|
'Accept:' . $mine, |
116
|
|
|
'Content-Type:' . $mine . ';charset=utf-8', |
117
|
|
|
'Authorization:' . $this->getAuthorization(), |
118
|
|
|
); |
119
|
|
|
$ch = curl_init($url); |
120
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
121
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); |
122
|
|
|
if ($method === 'post') { |
123
|
|
|
curl_setopt($ch, CURLOPT_POST, 1); |
124
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $body); |
125
|
|
|
} |
126
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
127
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
128
|
|
|
$result = curl_exec($ch); |
129
|
|
|
curl_close($ch); |
130
|
|
|
} else { |
131
|
|
|
$opts = array(); |
132
|
|
|
$opts['http'] = array(); |
133
|
|
|
$headers = array( |
134
|
|
|
'method' => strtoupper($method), |
135
|
|
|
); |
136
|
|
|
$headers[] = 'Accept:' . $mine; |
137
|
|
|
$headers['header'] = array(); |
138
|
|
|
$headers['header'][] = 'Authorization: ' . $this->getAuthorization(); |
139
|
|
|
$headers['header'][] = 'Content-Type:' . $mine . ';charset=utf-8'; |
140
|
|
|
|
141
|
|
|
if (!empty($body)) { |
142
|
|
|
$headers['header'][] = 'Content-Length:' . strlen($body); |
143
|
|
|
$headers['content'] = $body; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$opts['http'] = $headers; |
147
|
|
|
$result = file_get_contents($url, false, stream_context_create($opts)); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $result; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param $appId |
155
|
|
|
* @param $fromClient |
156
|
|
|
* @param string $to |
157
|
|
|
* @param null $fromSerNum |
158
|
|
|
* @param null $toSerNum |
159
|
|
|
* @param string $type |
160
|
|
|
* |
161
|
|
|
* @throws Exception |
162
|
|
|
* |
163
|
|
|
* @return mixed|string |
164
|
|
|
* @links http://www.ucpaas.com/page/doc/doc_rest3-1.jsp |
165
|
|
|
*/ |
166
|
|
|
public function callBack($appId, $fromClient, $to, $fromSerNum = null, $toSerNum = null, $type = 'json') |
167
|
|
|
{ |
168
|
|
|
$url = self::BaseUrl . self::SoftVersion . '/Accounts/' . $this->accountSid . '/Calls/callBack?sig=' . $this->getSigParameter(); |
169
|
|
|
if ($type === 'json') { |
170
|
|
|
$body_json = array('callback' => array( |
171
|
|
|
'appId' => $appId, |
172
|
|
|
'fromClient' => $fromClient, |
173
|
|
|
'fromSerNum' => $fromSerNum, |
174
|
|
|
'to' => $to, |
175
|
|
|
'toSerNum' => $toSerNum, |
176
|
|
|
)); |
177
|
|
|
$body = json_encode($body_json); |
178
|
|
|
} elseif ($type === 'xml') { |
179
|
|
|
$body_xml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
180
|
|
|
<callback> |
181
|
|
|
<fromClient>' . $fromClient . '</clientNumber> |
182
|
|
|
<fromSerNum>' . $fromSerNum . '</chargeType> |
183
|
|
|
<to>' . $to . '</charge> |
184
|
|
|
<toSerNum>' . $toSerNum . '</toSerNum> |
185
|
|
|
<appId>' . $appId . '</appId> |
186
|
|
|
</callback>'; |
187
|
|
|
$body = trim($body_xml); |
188
|
|
|
} else { |
189
|
|
|
throw new Exception('只能json或xml,默认为json'); |
190
|
|
|
} |
191
|
|
|
$data = $this->getResult($url, $body, $type, 'post'); |
192
|
|
|
|
193
|
|
|
return $data; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param $appId |
198
|
|
|
* @param $verifyCode |
199
|
|
|
* @param $to |
200
|
|
|
* @param string $type |
201
|
|
|
* |
202
|
|
|
* @throws Exception |
203
|
|
|
* |
204
|
|
|
* @return mixed|string |
205
|
|
|
* @links http://www.ucpaas.com/page/doc/doc_rest3-2.jsp |
206
|
|
|
*/ |
207
|
|
|
public function voiceCode($appId, $verifyCode, $to, $type = 'json') |
208
|
|
|
{ |
209
|
|
|
$url = self::BaseUrl . self::SoftVersion . '/Accounts/' . $this->accountSid . '/Calls/voiceCode?sig=' . $this->getSigParameter(); |
210
|
|
|
if ($type === 'json') { |
211
|
|
|
$body_json = array('voiceCode' => array( |
212
|
|
|
'appId' => $appId, |
213
|
|
|
'verifyCode' => $verifyCode, |
214
|
|
|
'to' => $to, |
215
|
|
|
)); |
216
|
|
|
$body = json_encode($body_json); |
217
|
|
|
} elseif ($type === 'xml') { |
218
|
|
|
$body_xml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
219
|
|
|
<voiceCode> |
220
|
|
|
<verifyCode>' . $verifyCode . '</clientNumber> |
221
|
|
|
<to>' . $to . '</charge> |
222
|
|
|
<appId>' . $appId . '</appId> |
223
|
|
|
</voiceCode>'; |
224
|
|
|
$body = trim($body_xml); |
225
|
|
|
} else { |
226
|
|
|
throw new Exception('只能json或xml,默认为json'); |
227
|
|
|
} |
228
|
|
|
$data = $this->getResult($url, $body, $type, 'post'); |
229
|
|
|
|
230
|
|
|
return $data; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @param $appId |
235
|
|
|
* @param $to |
236
|
|
|
* @param $templateId |
237
|
|
|
* @param null $param |
238
|
|
|
* @param string $type |
239
|
|
|
* |
240
|
|
|
* @throws Exception |
241
|
|
|
* |
242
|
|
|
* @return mixed|string |
243
|
|
|
* @links http://www.ucpaas.com/page/doc/doc_rest4-1.jsp |
244
|
|
|
*/ |
245
|
|
|
public function templateSMS($appId, $to, $templateId, $param = null, $type = 'json') |
246
|
|
|
{ |
247
|
|
|
$url = self::BaseUrl . self::SoftVersion . '/Accounts/' . $this->accountSid . '/Messages/templateSMS?sig=' . $this->getSigParameter(); |
248
|
|
|
if ($type === 'json') { |
249
|
|
|
$body_json = array('templateSMS' => array( |
250
|
|
|
'appId' => $appId, |
251
|
|
|
'templateId' => $templateId, |
252
|
|
|
'to' => $to, |
253
|
|
|
'param' => $param, |
254
|
|
|
)); |
255
|
|
|
$body = json_encode($body_json); |
256
|
|
|
} elseif ($type === 'xml') { |
257
|
|
|
$body_xml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
258
|
|
|
<templateSMS> |
259
|
|
|
<templateId>' . $templateId . '</templateId> |
260
|
|
|
<to>' . $to . '</to> |
261
|
|
|
<param>' . $param . '</param> |
262
|
|
|
<appId>' . $appId . '</appId> |
263
|
|
|
</templateSMS>'; |
264
|
|
|
$body = trim($body_xml); |
265
|
|
|
} else { |
266
|
|
|
throw new Exception('只能json或xml,默认为json'); |
267
|
|
|
} |
268
|
|
|
$data = $this->getResult($url, $body, $type, 'post'); |
269
|
|
|
|
270
|
|
|
return $data; |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.