1
|
|
|
<?php |
2
|
|
|
namespace Qiniu\Sms; |
3
|
|
|
|
4
|
|
|
use Qiniu\Http\Client; |
5
|
|
|
use Qiniu\Http\Error; |
6
|
|
|
use Qiniu\Config; |
7
|
|
|
use Qiniu\Auth; |
8
|
|
|
|
9
|
|
|
class Sms |
10
|
|
|
{ |
11
|
|
|
private $auth; |
12
|
|
|
private $baseURL; |
13
|
|
|
|
14
|
|
|
public function __construct(Auth $auth) |
15
|
|
|
{ |
16
|
|
|
$this->auth = $auth; |
17
|
|
|
|
18
|
|
|
$this->baseURL = sprintf("%s/%s/", Config::SMS_HOST, Config::SMS_VERSION); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/* |
22
|
|
|
* 创建签名 |
23
|
|
|
* signature: string 类型,必填,【长度限制8个字符内】超过长度会报错 |
24
|
|
|
* source: string 类型,必填,申请签名时必须指定签名来源。取值范围为: |
25
|
|
|
nterprises_and_institutions 企事业单位的全称或简称 |
26
|
|
|
website 工信部备案网站的全称或简称 |
27
|
|
|
app APP应用的全称或简称 |
28
|
|
|
public_number_or_small_program 公众号或小程序的全称或简称 |
29
|
|
|
store_name 电商平台店铺名的全称或简称 |
30
|
|
|
trade_name 商标名的全称或简称, |
31
|
|
|
* pics: 本地的图片路径 string 类型,可选 |
32
|
|
|
*@return: 类型array { |
33
|
|
|
"signature_id": <signature_id> |
34
|
|
|
} |
35
|
|
|
*/ |
36
|
|
|
public function createSignature(string $signature, string $source, string $pics = null) |
37
|
|
|
{ |
38
|
|
|
$params['signature'] = $signature; |
|
|
|
|
39
|
|
|
$params['source'] = $source; |
40
|
|
|
if (!empty($pics)) { |
41
|
|
|
$params['pics'] = $this->imgToBase64($pics); |
42
|
|
|
} |
43
|
|
|
$body = json_encode($params); |
44
|
|
|
$url =$this->baseURL.'signature'; |
45
|
|
|
$ret = $this->post($url, $body); |
46
|
|
|
return $ret; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/* |
50
|
|
|
* 编辑签名 |
51
|
|
|
* id 签名id : string 类型,必填, |
52
|
|
|
* signature: string 类型,必填, |
53
|
|
|
* source: string 类型,必填,申请签名时必须指定签名来源。取值范围为: |
54
|
|
|
enterprises_and_institutions 企事业单位的全称或简称 |
55
|
|
|
website 工信部备案网站的全称或简称 |
56
|
|
|
app APP应用的全称或简称 |
57
|
|
|
public_number_or_small_program 公众号或小程序的全称或简称 |
58
|
|
|
store_name 电商平台店铺名的全称或简称 |
59
|
|
|
trade_name 商标名的全称或简称, |
60
|
|
|
* pics: 本地的图片路径 string 类型,可选, |
61
|
|
|
* @return: 类型array { |
62
|
|
|
"signature": string |
63
|
|
|
} |
64
|
|
|
*/ |
65
|
|
|
public function updateSignature(string $id, string $signature, string $source, string $pics = null) |
66
|
|
|
{ |
67
|
|
|
$params['signature'] = $signature; |
|
|
|
|
68
|
|
|
$params['source'] = $source; |
69
|
|
|
if (!empty($pics)) { |
70
|
|
|
$params['pics'] = $this->imgToBase64($pics); |
71
|
|
|
} |
72
|
|
|
$body = json_encode($params); |
73
|
|
|
$url =$this->baseURL.'signature/'.$id; |
74
|
|
|
$ret = $this->PUT($url, $body); |
75
|
|
|
return $ret; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/* |
79
|
|
|
* 查询签名 |
80
|
|
|
* audit_status: 审核状态 string 类型,可选, |
81
|
|
|
取值范围为: "passed"(通过), "rejected"(未通过), "reviewing"(审核中) |
82
|
|
|
* page:页码 int 类型, |
83
|
|
|
* page_size: 分页大小 int 类型,可选, 默认为20 |
84
|
|
|
*@return: 类型array { |
85
|
|
|
"items": [{ |
86
|
|
|
"id": string, |
87
|
|
|
"signature": string, |
88
|
|
|
"source": string, |
89
|
|
|
"audit_status": string, |
90
|
|
|
"reject_reason": string, |
91
|
|
|
"created_at": int64, |
92
|
|
|
"updated_at": int64 |
93
|
|
|
}...], |
94
|
|
|
"total": int, |
95
|
|
|
"page": int, |
96
|
|
|
"page_size": int, |
97
|
|
|
} |
98
|
|
|
*/ |
99
|
|
|
public function checkSignature(string $audit_status = null, int $page = 1, int $page_size = 20) |
100
|
|
|
{ |
101
|
|
|
|
102
|
|
|
$url = sprintf( |
103
|
|
|
"%s?audit_status=%s&page=%s&page_size=%s", |
104
|
|
|
$this->baseURL.'signature', |
105
|
|
|
$audit_status, |
106
|
|
|
$page, |
107
|
|
|
$page_size |
108
|
|
|
); |
109
|
|
|
$ret = $this->get($url); |
110
|
|
|
return $ret; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
|
114
|
|
|
/* |
115
|
|
|
* 删除签名 |
116
|
|
|
* id 签名id string 类型,必填, |
117
|
|
|
* @retrun : 请求成功 HTTP 状态码为 200 |
118
|
|
|
*/ |
119
|
|
|
public function deleteSignature(string $id) |
120
|
|
|
{ |
121
|
|
|
$url = $this->baseURL . 'signature/' . $id; |
122
|
|
|
list(, $err) = $this->delete($url); |
123
|
|
|
return $err; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
|
128
|
|
|
|
129
|
|
|
/* |
130
|
|
|
* 创建模板 |
131
|
|
|
* name : 模板名称 string 类型 ,必填 |
132
|
|
|
* template: 模板内容 string 类型,必填 |
133
|
|
|
* type: 模板类型 string 类型,必填, |
134
|
|
|
取值范围为: notification (通知类短信), verification (验证码短信), marketing (营销类短信) |
135
|
|
|
* description: 申请理由简述 string 类型,必填 |
136
|
|
|
* signature_id: 已经审核通过的签名 string 类型,必填 |
137
|
|
|
* @return: 类型 array { |
138
|
|
|
"template_id": string |
139
|
|
|
} |
140
|
|
|
*/ |
141
|
|
|
public function createTemplate( |
142
|
|
|
string $name, |
143
|
|
|
string $template, |
144
|
|
|
string $type, |
145
|
|
|
string $description, |
146
|
|
|
string $signture_id |
147
|
|
|
) { |
148
|
|
|
$params['name'] = $name; |
|
|
|
|
149
|
|
|
$params['template'] = $template; |
150
|
|
|
$params['type'] = $type; |
151
|
|
|
$params['description'] = $description; |
152
|
|
|
$params['signature_id'] = $signture_id; |
153
|
|
|
|
154
|
|
|
$body = json_encode($params); |
155
|
|
|
$url =$this->baseURL.'template'; |
156
|
|
|
$ret = $this->post($url, $body); |
157
|
|
|
return $ret; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/* |
161
|
|
|
* 查询模板 |
162
|
|
|
* audit_status: 审核状态 string 类型 ,可选, |
163
|
|
|
取值范围为: passed (通过), rejected (未通过), reviewing (审核中) |
164
|
|
|
* page: 页码 int 类型,可选,默认为 1 |
165
|
|
|
* page_size: 分页大小 int 类型,可选,默认为 20 |
166
|
|
|
* @return: 类型array{ |
167
|
|
|
"items": [{ |
168
|
|
|
"id": string, |
169
|
|
|
"name": string, |
170
|
|
|
"template": string, |
171
|
|
|
"audit_status": string, |
172
|
|
|
"reject_reason": string, |
173
|
|
|
"type": string, |
174
|
|
|
"signature_id": string, // 模版绑定的签名ID |
175
|
|
|
"signature_text": string, // 模版绑定的签名内容 |
176
|
|
|
"created_at": int64, |
177
|
|
|
"updated_at": int64 |
178
|
|
|
}...], |
179
|
|
|
"total": int, |
180
|
|
|
"page": int, |
181
|
|
|
"page_size": int |
182
|
|
|
} |
183
|
|
|
*/ |
184
|
|
|
public function queryTemplate(string $audit_status = null, int $page = 1, int $page_size = 20) |
185
|
|
|
{ |
186
|
|
|
|
187
|
|
|
$url = sprintf( |
188
|
|
|
"%s?audit_status=%s&page=%s&page_size=%s", |
189
|
|
|
$this->baseURL.'template', |
190
|
|
|
$audit_status, |
191
|
|
|
$page, |
192
|
|
|
$page_size |
193
|
|
|
); |
194
|
|
|
$ret = $this->get($url); |
195
|
|
|
return $ret; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/* |
199
|
|
|
* 编辑模板 |
200
|
|
|
* id :模板id |
201
|
|
|
* name : 模板名称 string 类型 ,必填 |
202
|
|
|
* template: 模板内容 string 类型,必填 |
203
|
|
|
* description: 申请理由简述 string 类型,必填 |
204
|
|
|
* signature_id: 已经审核通过的签名 string 类型,必填 |
205
|
|
|
* @retrun : 请求成功 HTTP 状态码为 200 |
206
|
|
|
*/ |
207
|
|
|
public function updateTemplate( |
208
|
|
|
string $id, |
209
|
|
|
string $name, |
210
|
|
|
string $template, |
211
|
|
|
string $description, |
212
|
|
|
string $signature_id |
213
|
|
|
) { |
214
|
|
|
$params['name'] = $name; |
|
|
|
|
215
|
|
|
$params['template'] = $template; |
216
|
|
|
$params['description'] = $description; |
217
|
|
|
$params['signature_id'] = $signature_id; |
218
|
|
|
$body = json_encode($params); |
219
|
|
|
$url =$this->baseURL.'template/'.$id; |
220
|
|
|
$ret = $this->PUT($url, $body); |
221
|
|
|
return $ret; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/* |
225
|
|
|
* 删除模板 |
226
|
|
|
* id :模板id string 类型,必填, |
227
|
|
|
* @retrun : 请求成功 HTTP 状态码为 200 |
228
|
|
|
*/ |
229
|
|
|
public function deleteTemplate(string $id) |
230
|
|
|
{ |
231
|
|
|
$url = $this->baseURL . 'template/' . $id; |
232
|
|
|
list(, $err) = $this->delete($url); |
233
|
|
|
return $err; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/* |
237
|
|
|
* 发送短信 |
238
|
|
|
* 编辑模板 |
239
|
|
|
* template_id :模板id string类型,必填 |
240
|
|
|
* mobiles : 手机号数组 []string 类型 ,必填 |
241
|
|
|
* parameters: 模板内容 map[string]string 类型,可选 |
242
|
|
|
* @return: 类型json { |
243
|
|
|
"job_id": string |
244
|
|
|
} |
245
|
|
|
*/ |
246
|
|
|
public function sendMessage(string $template_id, array $mobiles, array $parameters = null) |
247
|
|
|
{ |
248
|
|
|
$params['template_id'] = $template_id; |
|
|
|
|
249
|
|
|
$params['mobiles'] = $mobiles; |
250
|
|
|
if (!empty($parameters)) { |
251
|
|
|
$params['parameters'] = $parameters; |
252
|
|
|
} |
253
|
|
|
$body = json_encode($params); |
254
|
|
|
$url =$this->baseURL.'message'; |
255
|
|
|
$ret = $this->post($url, $body); |
256
|
|
|
return $ret; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
public function imgToBase64(string $img_file) |
260
|
|
|
{ |
261
|
|
|
$img_base64 = ''; |
262
|
|
|
if (file_exists($img_file)) { |
263
|
|
|
$app_img_file = $img_file; // 图片路径 |
264
|
|
|
$img_info = getimagesize($app_img_file); // 取得图片的大小,类型等 |
265
|
|
|
$fp = fopen($app_img_file, "r"); // 图片是否可读权限 |
266
|
|
|
if ($fp) { |
267
|
|
|
$filesize = filesize($app_img_file); |
268
|
|
|
if ($filesize > 5*1024*1024) { |
269
|
|
|
die("pic size < 5M !"); |
270
|
|
|
} |
271
|
|
|
$content = fread($fp, $filesize); |
272
|
|
|
$file_content = chunk_split(base64_encode($content)); // base64编码 |
273
|
|
|
switch ($img_info[2]) { //判读图片类型 |
274
|
|
|
case 1: |
275
|
|
|
$img_type = 'gif'; |
276
|
|
|
break; |
277
|
|
|
case 2: |
278
|
|
|
$img_type = 'jpg'; |
279
|
|
|
break; |
280
|
|
|
case 3: |
281
|
|
|
$img_type = 'png'; |
282
|
|
|
break; |
283
|
|
|
} |
284
|
|
|
//合成图片的base64编码 |
285
|
|
|
$img_base64 = 'data:image/' . $img_type . ';base64,' . $file_content; |
|
|
|
|
286
|
|
|
} |
287
|
|
|
fclose($fp); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
return $img_base64; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
private function get($url, $cType = null) |
294
|
|
|
{ |
295
|
|
|
$rtcToken = $this->auth->authorizationV2($url, "GET", null, $cType); |
296
|
|
|
$rtcToken['Content-Type'] = $cType; |
297
|
|
|
$ret = Client::get($url, $rtcToken); |
298
|
|
|
if (!$ret->ok()) { |
299
|
|
|
return array(null, new Error($url, $ret)); |
300
|
|
|
} |
301
|
|
|
return array($ret->json(), null); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
private function delete($url, $contentType = 'application/json') |
305
|
|
|
{ |
306
|
|
|
$rtcToken = $this->auth->authorizationV2($url, "DELETE", null, $contentType); |
307
|
|
|
$rtcToken['Content-Type'] = $contentType; |
308
|
|
|
$ret = Client::delete($url, $rtcToken); |
309
|
|
|
if (!$ret->ok()) { |
310
|
|
|
return array(null, new Error($url, $ret)); |
311
|
|
|
} |
312
|
|
|
return array($ret->json(), null); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
private function post($url, $body, $contentType = 'application/json') |
316
|
|
|
{ |
317
|
|
|
$rtcToken = $this->auth->authorizationV2($url, "POST", $body, $contentType); |
318
|
|
|
$rtcToken['Content-Type'] = $contentType; |
319
|
|
|
$ret = Client::post($url, $body, $rtcToken); |
320
|
|
|
if (!$ret->ok()) { |
321
|
|
|
return array(null, new Error($url, $ret)); |
322
|
|
|
} |
323
|
|
|
$r = ($ret->body === null) ? array() : $ret->json(); |
324
|
|
|
return array($r, null); |
325
|
|
|
} |
326
|
|
|
private function PUT($url, $body, $contentType = 'application/json') |
327
|
|
|
{ |
328
|
|
|
$rtcToken = $this->auth->authorizationV2($url, "PUT", $body, $contentType); |
329
|
|
|
$rtcToken['Content-Type'] = $contentType; |
330
|
|
|
$ret = Client::put($url, $body, $rtcToken); |
331
|
|
|
if (!$ret->ok()) { |
332
|
|
|
return array(null, new Error($url, $ret)); |
333
|
|
|
} |
334
|
|
|
$r = ($ret->body === null) ? array() : $ret->json(); |
335
|
|
|
return array($r, null); |
336
|
|
|
} |
337
|
|
|
} |
338
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.