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 类型,必填, |
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
|
|
|
*/ |
33
|
|
|
public function createSignature($signature, $source, $pics = null) |
34
|
|
|
{ |
35
|
|
|
$params['signature'] = $signature; |
|
|
|
|
36
|
|
|
$params['source'] = $source; |
37
|
|
|
if (!empty($pics)) { |
38
|
|
|
$params['pics'] = $this->imgToBase64($pics); |
39
|
|
|
} |
40
|
|
|
$body = json_encode($params); |
41
|
|
|
$url =$this->baseURL."signature"; |
42
|
|
|
$ret = $this->post($url, $body); |
43
|
|
|
return $ret; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/* |
47
|
|
|
* 编辑签名 |
48
|
|
|
* id 签名id |
49
|
|
|
* signature: string 类型,必填, |
50
|
|
|
* source: string 类型,必填,申请签名时必须指定签名来源。取值范围为: |
51
|
|
|
enterprises_and_institutions 企事业单位的全称或简称 |
52
|
|
|
website 工信部备案网站的全称或简称 |
53
|
|
|
app APP应用的全称或简称 |
54
|
|
|
public_number_or_small_program 公众号或小程序的全称或简称 |
55
|
|
|
store_name 电商平台店铺名的全称或简称 |
56
|
|
|
trade_name 商标名的全称或简称, |
57
|
|
|
* pics: 本地的图片路径 string 类型,可选, |
58
|
|
|
*/ |
59
|
|
|
public function updateSignature($id, $signature, $source, $pics = null) |
60
|
|
|
{ |
61
|
|
|
$params['signature'] = $signature; |
|
|
|
|
62
|
|
|
$params['source'] = $source; |
63
|
|
|
if (!empty($pics)) { |
64
|
|
|
$params['pics'] = $this->imgToBase64($pics); |
65
|
|
|
} |
66
|
|
|
$body = json_encode($params); |
67
|
|
|
$url =$this->baseURL."signature/".$id; |
68
|
|
|
$ret = $this->PUT($url, $body); |
69
|
|
|
return $ret; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/* |
73
|
|
|
* 查询签名 |
74
|
|
|
* audit_status: 审核状态 string 类型,可选, |
75
|
|
|
取值范围为: "passed"(通过), "rejected"(未通过), "reviewing"(审核中) |
76
|
|
|
* page:页码 int 类型, |
77
|
|
|
* maxUsers:人数限制 ,可选,默认为 1 |
78
|
|
|
* page_size: 分页大小 int 类型,可选, 默认为20 |
79
|
|
|
*/ |
80
|
|
|
public function checkSignature($audit_status = null, $page = 1, $page_size = 20) |
81
|
|
|
{ |
82
|
|
|
|
83
|
|
|
$url = sprintf( |
84
|
|
|
"%s?audit_status=%s&page=%s&page_size=%s", |
85
|
|
|
$this->baseURL."signature", |
86
|
|
|
$audit_status, |
87
|
|
|
$page, |
88
|
|
|
$page_size |
89
|
|
|
); |
90
|
|
|
$ret = $this->get($url); |
91
|
|
|
return $ret; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
/* |
96
|
|
|
* 删除签名 |
97
|
|
|
* id 签名id |
98
|
|
|
*/ |
99
|
|
|
public function deleteSignature($id) |
100
|
|
|
{ |
101
|
|
|
$url = $this->baseURL . 'signature/' . $id; |
102
|
|
|
list(, $err) = $this->delete($url); |
103
|
|
|
return $err; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
|
108
|
|
|
|
109
|
|
|
/* |
110
|
|
|
* 创建模板 |
111
|
|
|
* name : 模板名称 string 类型 ,必填 |
112
|
|
|
* template: 模板内容 string 类型,必填 |
113
|
|
|
* type: 模板类型 string 类型,必填, |
114
|
|
|
取值范围为: notification (通知类短信), verification (验证码短信), marketing (营销类短信) |
115
|
|
|
* description: 申请理由简述 string 类型,必填 |
116
|
|
|
* signature_id: 已经审核通过的签名 string 类型,必填 |
117
|
|
|
*/ |
118
|
|
|
public function createTemplate($name, $template, $type, $description, $signture_id) |
119
|
|
|
{ |
120
|
|
|
$params['name'] = $name; |
|
|
|
|
121
|
|
|
$params['template'] = $template; |
122
|
|
|
$params['type'] = $type; |
123
|
|
|
$params['description'] = $description; |
124
|
|
|
$params['signature_id'] = $signture_id; |
125
|
|
|
|
126
|
|
|
$body = json_encode($params); |
127
|
|
|
var_dump($body); |
|
|
|
|
128
|
|
|
$url =$this->baseURL."template"; |
129
|
|
|
$ret = $this->post($url, $body); |
130
|
|
|
return $ret; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/* |
134
|
|
|
* 查询模板 |
135
|
|
|
* audit_status: 审核状态 string 类型 ,可选, |
136
|
|
|
取值范围为: passed (通过), rejected (未通过), reviewing (审核中) |
137
|
|
|
* page: 页码 int 类型,可选,默认为 1 |
138
|
|
|
* page_size: 分页大小 int 类型,可选,默认为 20 |
139
|
|
|
*/ |
140
|
|
|
public function queryTemplate($audit_status = null, $page = 1, $page_size = 20) |
141
|
|
|
{ |
142
|
|
|
|
143
|
|
|
$url = sprintf( |
144
|
|
|
"%s?audit_status=%s&page=%s&page_size=%s", |
145
|
|
|
$this->baseURL."template", |
146
|
|
|
$audit_status, |
147
|
|
|
$page, |
148
|
|
|
$page_size |
149
|
|
|
); |
150
|
|
|
$ret = $this->get($url); |
151
|
|
|
return $ret; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/* |
155
|
|
|
* 编辑模板 |
156
|
|
|
* id :模板id |
157
|
|
|
* name : 模板名称 string 类型 ,必填 |
158
|
|
|
* template: 模板内容 string 类型,必填 |
159
|
|
|
* description: 申请理由简述 string 类型,必填 |
160
|
|
|
* signature_id: 已经审核通过的签名 string 类型,必填 |
161
|
|
|
*/ |
162
|
|
|
public function updateTemplate($id, $name, $template, $description, $signature_id) |
163
|
|
|
{ |
164
|
|
|
$params['name'] = $name; |
|
|
|
|
165
|
|
|
$params['template'] = $template; |
166
|
|
|
$params['description'] = $description; |
167
|
|
|
$params['signature_id'] = $signature_id; |
168
|
|
|
$body = json_encode($params); |
169
|
|
|
$url =$this->baseURL."template/".$id; |
170
|
|
|
$ret = $this->PUT($url, $body); |
171
|
|
|
return $ret; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/* |
175
|
|
|
* 删除模板 |
176
|
|
|
* id :模板id |
177
|
|
|
*/ |
178
|
|
|
public function deleteTemplate($id) |
179
|
|
|
{ |
180
|
|
|
$url = $this->baseURL . 'template/' . $id; |
181
|
|
|
list(, $err) = $this->delete($url); |
182
|
|
|
return $err; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/* |
186
|
|
|
* 发送短信 |
187
|
|
|
* 编辑模板 |
188
|
|
|
* template_id :模板id string类型,必填 |
189
|
|
|
* mobiles : 手机号数组 []string 类型 ,必填 |
190
|
|
|
* parameters: 模板内容 map[string]string 类型,可选 |
191
|
|
|
*/ |
192
|
|
|
public function sendMessage($template_id, $mobiles, array $parameters = null) |
193
|
|
|
{ |
194
|
|
|
$params['template_id'] = $template_id; |
|
|
|
|
195
|
|
|
$params['mobiles'] = $mobiles; |
196
|
|
|
if (!empty($parameters)) { |
197
|
|
|
$params['parameters'] = $parameters; |
198
|
|
|
} |
199
|
|
|
$body = json_encode($params); |
200
|
|
|
$url =$this->baseURL."message"; |
201
|
|
|
$ret = $this->post($url, $body); |
202
|
|
|
return $ret; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function imgToBase64($img_file) |
206
|
|
|
{ |
207
|
|
|
$img_base64 = ''; |
208
|
|
|
if (file_exists($img_file)) { |
209
|
|
|
$app_img_file = $img_file; // 图片路径 |
210
|
|
|
$img_info = getimagesize($app_img_file); // 取得图片的大小,类型等 |
211
|
|
|
$fp = fopen($app_img_file, "r"); // 图片是否可读权限 |
212
|
|
|
if ($fp) { |
213
|
|
|
$filesize = filesize($app_img_file); |
214
|
|
|
if ($filesize > 5*1024*1024) { |
215
|
|
|
new Error("pic size < 5M !"); |
|
|
|
|
216
|
|
|
} |
217
|
|
|
$content = fread($fp, $filesize); |
218
|
|
|
$file_content = chunk_split(base64_encode($content)); // base64编码 |
219
|
|
|
switch ($img_info[2]) { //判读图片类型 |
220
|
|
|
case 1: |
221
|
|
|
$img_type = "gif"; |
222
|
|
|
break; |
223
|
|
|
case 2: |
224
|
|
|
$img_type = "jpg"; |
225
|
|
|
break; |
226
|
|
|
case 3: |
227
|
|
|
$img_type = "png"; |
228
|
|
|
break; |
229
|
|
|
} |
230
|
|
|
//合成图片的base64编码 |
231
|
|
|
$img_base64 = 'data:image/' . $img_type . ';base64,' . $file_content; |
|
|
|
|
232
|
|
|
} |
233
|
|
|
fclose($fp); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
return $img_base64; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
private function get($url, $cType = null) |
240
|
|
|
{ |
241
|
|
|
$rtcToken = $this->auth->authorizationV2($url, "GET", null, $cType); |
242
|
|
|
$rtcToken['Content-Type'] = $cType; |
243
|
|
|
$ret = Client::get($url, $rtcToken); |
244
|
|
|
if (!$ret->ok()) { |
245
|
|
|
return array(null, new Error($url, $ret)); |
246
|
|
|
} |
247
|
|
|
return array($ret->json(), null); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
private function delete($url, $contentType = 'application/json') |
251
|
|
|
{ |
252
|
|
|
$rtcToken = $this->auth->authorizationV2($url, "DELETE", null, $contentType); |
253
|
|
|
$rtcToken['Content-Type'] = $contentType; |
254
|
|
|
$ret = Client::delete($url, $rtcToken); |
255
|
|
|
if (!$ret->ok()) { |
256
|
|
|
return array(null, new Error($url, $ret)); |
257
|
|
|
} |
258
|
|
|
return array($ret->json(), null); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
private function post($url, $body, $contentType = 'application/json') |
262
|
|
|
{ |
263
|
|
|
$rtcToken = $this->auth->authorizationV2($url, "POST", $body, $contentType); |
264
|
|
|
$rtcToken['Content-Type'] = $contentType; |
265
|
|
|
$ret = Client::post($url, $body, $rtcToken); |
266
|
|
|
if (!$ret->ok()) { |
267
|
|
|
return array(null, new Error($url, $ret)); |
268
|
|
|
} |
269
|
|
|
$r = ($ret->body === null) ? array() : $ret->json(); |
270
|
|
|
return array($r, null); |
271
|
|
|
} |
272
|
|
|
private function PUT($url, $body, $contentType = 'application/json') |
273
|
|
|
{ |
274
|
|
|
$rtcToken = $this->auth->authorizationV2($url, "PUT", $body, $contentType); |
275
|
|
|
$rtcToken['Content-Type'] = $contentType; |
276
|
|
|
$ret = Client::put($url, $body, $rtcToken); |
277
|
|
|
if (!$ret->ok()) { |
278
|
|
|
return array(null, new Error($url, $ret)); |
279
|
|
|
} |
280
|
|
|
$r = ($ret->body === null) ? array() : $ret->json(); |
281
|
|
|
return array($r, null); |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
|
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.