Sms.php
10.5 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
<?php
namespace Qiniu\Sms;
use Qiniu\Http\Client;
use Qiniu\Http\Error;
use Qiniu\Config;
use Qiniu\Auth;
class Sms
{
private $auth;
private $baseURL;
public function __construct(Auth $auth)
{
$this->auth = $auth;
$this->baseURL = sprintf("%s/%s/", Config::SMS_HOST, Config::SMS_VERSION);
}
/*
* 创建签名
* signature: string 类型,必填,【长度限制8个字符内】超过长度会报错
* source: string 类型,必填,申请签名时必须指定签名来源。取值范围为:
nterprises_and_institutions 企事业单位的全称或简称
website 工信部备案网站的全称或简称
app APP应用的全称或简称
public_number_or_small_program 公众号或小程序的全称或简称
store_name 电商平台店铺名的全称或简称
trade_name 商标名的全称或简称,
* pics: 本地的图片路径 string 类型,可选
*@return: 类型array {
"signature_id": <signature_id>
}
*/
public function createSignature($signature, $source, $pics = null)
{
$params['signature'] = $signature;
$params['source'] = $source;
if (!empty($pics)) {
$params['pics'] = $this->imgToBase64($pics);
}
$body = json_encode($params);
$url =$this->baseURL.'signature';
$ret = $this->post($url, $body);
return $ret;
}
/*
* 编辑签名
* id 签名id : string 类型,必填,
* signature: string 类型,必填,
* source: string 类型,必填,申请签名时必须指定签名来源。取值范围为:
enterprises_and_institutions 企事业单位的全称或简称
website 工信部备案网站的全称或简称
app APP应用的全称或简称
public_number_or_small_program 公众号或小程序的全称或简称
store_name 电商平台店铺名的全称或简称
trade_name 商标名的全称或简称,
* pics: 本地的图片路径 string 类型,可选,
* @return: 类型array {
"signature": string
}
*/
public function updateSignature($id, $signature, $source, $pics = null)
{
$params['signature'] = $signature;
$params['source'] = $source;
if (!empty($pics)) {
$params['pics'] = $this->imgToBase64($pics);
}
$body = json_encode($params);
$url =$this->baseURL.'signature/'.$id;
$ret = $this->PUT($url, $body);
return $ret;
}
/*
* 查询签名
* audit_status: 审核状态 string 类型,可选,
取值范围为: "passed"(通过), "rejected"(未通过), "reviewing"(审核中)
* page:页码 int 类型,
* page_size: 分页大小 int 类型,可选, 默认为20
*@return: 类型array {
"items": [{
"id": string,
"signature": string,
"source": string,
"audit_status": string,
"reject_reason": string,
"created_at": int64,
"updated_at": int64
}...],
"total": int,
"page": int,
"page_size": int,
}
*/
public function checkSignature($audit_status = null, $page = 1, $page_size = 20)
{
$url = sprintf(
"%s?audit_status=%s&page=%s&page_size=%s",
$this->baseURL.'signature',
$audit_status,
$page,
$page_size
);
$ret = $this->get($url);
return $ret;
}
/*
* 删除签名
* id 签名id string 类型,必填,
* @retrun : 请求成功 HTTP 状态码为 200
*/
public function deleteSignature($id)
{
$url = $this->baseURL . 'signature/' . $id;
list(, $err) = $this->delete($url);
return $err;
}
/*
* 创建模板
* name : 模板名称 string 类型 ,必填
* template: 模板内容 string 类型,必填
* type: 模板类型 string 类型,必填,
取值范围为: notification (通知类短信), verification (验证码短信), marketing (营销类短信)
* description: 申请理由简述 string 类型,必填
* signature_id: 已经审核通过的签名 string 类型,必填
* @return: 类型 array {
"template_id": string
}
*/
public function createTemplate(
$name,
$template,
$type,
$description,
$signture_id
) {
$params['name'] = $name;
$params['template'] = $template;
$params['type'] = $type;
$params['description'] = $description;
$params['signature_id'] = $signture_id;
$body = json_encode($params);
$url =$this->baseURL.'template';
$ret = $this->post($url, $body);
return $ret;
}
/*
* 查询模板
* audit_status: 审核状态 string 类型 ,可选,
取值范围为: passed (通过), rejected (未通过), reviewing (审核中)
* page: 页码 int 类型,可选,默认为 1
* page_size: 分页大小 int 类型,可选,默认为 20
* @return: 类型array{
"items": [{
"id": string,
"name": string,
"template": string,
"audit_status": string,
"reject_reason": string,
"type": string,
"signature_id": string, // 模版绑定的签名ID
"signature_text": string, // 模版绑定的签名内容
"created_at": int64,
"updated_at": int64
}...],
"total": int,
"page": int,
"page_size": int
}
*/
public function queryTemplate($audit_status = null, $page = 1, $page_size = 20)
{
$url = sprintf(
"%s?audit_status=%s&page=%s&page_size=%s",
$this->baseURL.'template',
$audit_status,
$page,
$page_size
);
$ret = $this->get($url);
return $ret;
}
/*
* 编辑模板
* id :模板id
* name : 模板名称 string 类型 ,必填
* template: 模板内容 string 类型,必填
* description: 申请理由简述 string 类型,必填
* signature_id: 已经审核通过的签名 string 类型,必填
* @retrun : 请求成功 HTTP 状态码为 200
*/
public function updateTemplate(
$id,
$name,
$template,
$description,
$signature_id
) {
$params['name'] = $name;
$params['template'] = $template;
$params['description'] = $description;
$params['signature_id'] = $signature_id;
$body = json_encode($params);
$url =$this->baseURL.'template/'.$id;
$ret = $this->PUT($url, $body);
return $ret;
}
/*
* 删除模板
* id :模板id string 类型,必填,
* @retrun : 请求成功 HTTP 状态码为 200
*/
public function deleteTemplate($id)
{
$url = $this->baseURL . 'template/' . $id;
list(, $err) = $this->delete($url);
return $err;
}
/*
* 发送短信
* 编辑模板
* template_id :模板id string类型,必填
* mobiles : 手机号数组 []string 类型 ,必填
* parameters: 模板内容 map[string]string 类型,可选
* @return: 类型json {
"job_id": string
}
*/
public function sendMessage($template_id, $mobiles, $parameters = null)
{
$params['template_id'] = $template_id;
$params['mobiles'] = $mobiles;
if (!empty($parameters)) {
$params['parameters'] = $parameters;
}
$body = json_encode($params);
$url =$this->baseURL.'message';
$ret = $this->post($url, $body);
return $ret;
}
public function imgToBase64($img_file)
{
$img_base64 = '';
if (file_exists($img_file)) {
$app_img_file = $img_file; // 图片路径
$img_info = getimagesize($app_img_file); // 取得图片的大小,类型等
$fp = fopen($app_img_file, "r"); // 图片是否可读权限
if ($fp) {
$filesize = filesize($app_img_file);
if ($filesize > 5*1024*1024) {
die("pic size < 5M !");
}
$content = fread($fp, $filesize);
$file_content = chunk_split(base64_encode($content)); // base64编码
switch ($img_info[2]) { //判读图片类型
case 1:
$img_type = 'gif';
break;
case 2:
$img_type = 'jpg';
break;
case 3:
$img_type = 'png';
break;
}
//合成图片的base64编码
$img_base64 = 'data:image/' . $img_type . ';base64,' . $file_content;
}
fclose($fp);
}
return $img_base64;
}
private function get($url, $cType = null)
{
$rtcToken = $this->auth->authorizationV2($url, "GET", null, $cType);
$rtcToken['Content-Type'] = $cType;
$ret = Client::get($url, $rtcToken);
if (!$ret->ok()) {
return array(null, new Error($url, $ret));
}
return array($ret->json(), null);
}
private function delete($url, $contentType = 'application/json')
{
$rtcToken = $this->auth->authorizationV2($url, "DELETE", null, $contentType);
$rtcToken['Content-Type'] = $contentType;
$ret = Client::delete($url, $rtcToken);
if (!$ret->ok()) {
return array(null, new Error($url, $ret));
}
return array($ret->json(), null);
}
private function post($url, $body, $contentType = 'application/json')
{
$rtcToken = $this->auth->authorizationV2($url, "POST", $body, $contentType);
$rtcToken['Content-Type'] = $contentType;
$ret = Client::post($url, $body, $rtcToken);
if (!$ret->ok()) {
return array(null, new Error($url, $ret));
}
$r = ($ret->body === null) ? array() : $ret->json();
return array($r, null);
}
private function PUT($url, $body, $contentType = 'application/json')
{
$rtcToken = $this->auth->authorizationV2($url, "PUT", $body, $contentType);
$rtcToken['Content-Type'] = $contentType;
$ret = Client::put($url, $body, $rtcToken);
if (!$ret->ok()) {
return array(null, new Error($url, $ret));
}
$r = ($ret->body === null) ? array() : $ret->json();
return array($r, null);
}
}