Issues (76)

src/Qiniu/Sms/Sms.php (1 issue)

1
<?php
2
3
namespace Qiniu\Sms;
4
5
use Qiniu\Auth;
6
use Qiniu\Config;
7
use Qiniu\Http\Error;
8
use Qiniu\Http\Client;
9
use Qiniu\Http\Proxy;
10
11
class Sms
12
{
13
    private $auth;
14
    private $baseURL;
15
    private $proxy;
16
17
    public function __construct(Auth $auth, $proxy = null, $proxy_auth = null, $proxy_user_password = null)
18
    {
19
        $this->auth = $auth;
20
        $this->baseURL = sprintf("%s/%s/", Config::SMS_HOST, Config::SMS_VERSION);
21
        $this->proxy = new Proxy($proxy, $proxy_auth, $proxy_user_password);
22
    }
23
24
    /**
25
     * 创建签名
26
     *
27
     * @param string $signature 签名
28
     * @param string $source 签名来源,申请签名时必须指定签名来源
29
     * @param string $pics 签名对应的资质证明图片进行 base64 编码格式转换后的字符串,可选
30
     * @return array
31
     *
32
     * @link https://developer.qiniu.com/sms/api/5844/sms-api-create-signature
33
     */
34
    public function createSignature($signature, $source, $pics = null)
35
    {
36
        $params = array();
37
        $params['signature'] = $signature;
38
        $params['source'] = $source;
39
        if (!empty($pics)) {
40
            $params['pics'] = array($this->imgToBase64($pics));
41
        }
42
        $body = json_encode($params);
43
        $url = $this->baseURL . 'signature';
44
        return $this->post($url, $body);
45
    }
46
47
    /**
48
     * 编辑签名
49
     *
50
     * @param string $id 签名 ID
51
     * @param string $signature 签名
52
     * @param string $source 签名来源
53
     * @param string $pics 签名对应的资质证明图片进行 base64 编码格式转换后的字符串,可选
54
     * @return array
55
     * @link https://developer.qiniu.com/sms/api/5890/sms-api-edit-signature
56
     */
57
    public function updateSignature($id, $signature, $source, $pics = null)
58
    {
59
        $params = array();
60
        $params['signature'] = $signature;
61
        $params['source'] = $source;
62
        if (!empty($pics)) {
63
            $params['pics'] = array($this->imgToBase64($pics));
64
        }
65
        $body = json_encode($params);
66
        $url = $this->baseURL . 'signature/' . $id;
67
        return $this->PUT($url, $body);
68
    }
69
70
    /**
71
     * 列出签名
72
     *
73
     * @param string $audit_status 审核状态:"passed"(通过), "rejected"(未通过), "reviewing"(审核中)
74
     * @param int $page 页码。默认为 1
75
     * @param int $page_size 分页大小。默认为 20
76
     * @return array
77
     * @link https://developer.qiniu.com/sms/api/5889/sms-api-query-signature
78
     */
79
    public function querySignature($audit_status = null, $page = 1, $page_size = 20)
80
    {
81
82
        $url = sprintf(
83
            "%s?audit_status=%s&page=%s&page_size=%s",
84
            $this->baseURL . 'signature',
85
            $audit_status,
86
            $page,
87
            $page_size
88
        );
89
        return $this->get($url);
90
    }
91
92
    /**
93
     * 查询单个签名
94
     *
95
     * @param string $signature_id
96
     * @return array
97
     * @link https://developer.qiniu.com/sms/api/5970/query-a-single-signature
98
     */
99
    public function checkSingleSignature($signature_id)
100
    {
101
102
        $url = sprintf(
103
            "%s/%s",
104
            $this->baseURL . 'signature',
105
            $signature_id
106
        );
107
        return $this->get($url);
108
    }
109
110
    /**
111
     * 删除签名
112
     *
113
     * @param string $signature_id 签名 ID
114
     * @return array
115
     * @link https://developer.qiniu.com/sms/api/5891/sms-api-delete-signature
116
     */
117
    public function deleteSignature($signature_id)
118
    {
119
        $url = $this->baseURL . 'signature/' . $signature_id;
120
        return $this->delete($url);
121
    }
122
123
    /**
124
     * 创建模板
125
     *
126
     * @param string $name 模板名称
127
     * @param string $template 模板内容 可设置自定义变量,发送短信时候使用,参考:${code}
128
     * @param string $type notification:通知类,verification:验证码,marketing:营销类,voice:语音类
129
     * @param string $description 申请理由简述
130
     * @param string $signature_id 已经审核通过的签名
131
     * @return array array
132
     * @link https://developer.qiniu.com/sms/api/5893/sms-api-create-template
133
     */
134
    public function createTemplate(
135
        $name,
136
        $template,
137
        $type,
138
        $description,
139
        $signature_id
140
    ) {
141
        $params = array();
142
        $params['name'] = $name;
143
        $params['template'] = $template;
144
        $params['type'] = $type;
145
        $params['description'] = $description;
146
        $params['signature_id'] = $signature_id;
147
148
        $body = json_encode($params);
149
        $url = $this->baseURL . 'template';
150
        return $this->post($url, $body);
151
    }
152
153
    /**
154
     * 列出模板
155
     *
156
     * @param string $audit_status 审核状态:passed (通过), rejected (未通过), reviewing (审核中)
157
     * @param int $page 页码。默认为 1
158
     * @param int $page_size 分页大小。默认为 20
159
     * @return array
160
     * @link https://developer.qiniu.com/sms/api/5894/sms-api-query-template
161
     */
162
    public function queryTemplate($audit_status = null, $page = 1, $page_size = 20)
163
    {
164
165
        $url = sprintf(
166
            "%s?audit_status=%s&page=%s&page_size=%s",
167
            $this->baseURL . 'template',
168
            $audit_status,
169
            $page,
170
            $page_size
171
        );
172
        return $this->get($url);
173
    }
174
175
    /**
176
     * 查询单个模版
177
     *
178
     * @param string $template_id 模版ID
179
     * @return array
180
     * @link https://developer.qiniu.com/sms/api/5969/query-a-single-template
181
     */
182
    public function querySingleTemplate($template_id)
183
    {
184
185
        $url = sprintf(
186
            "%s/%s",
187
            $this->baseURL . 'template',
188
            $template_id
189
        );
190
        return $this->get($url);
191
    }
192
193
    /**
194
     * 编辑模板
195
     *
196
     * @param string $id 模板 ID
197
     * @param string $name 模板名称
198
     * @param string $template 模板内容
199
     * @param string $description 申请理由简述
200
     * @param string $signature_id 已经审核通过的签名 ID
201
     * @return array
202
     * @link https://developer.qiniu.com/sms/api/5895/sms-api-edit-template
203
     */
204
    public function updateTemplate(
205
        $id,
206
        $name,
207
        $template,
208
        $description,
209
        $signature_id
210
    ) {
211
        $params = array();
212
        $params['name'] = $name;
213
        $params['template'] = $template;
214
        $params['description'] = $description;
215
        $params['signature_id'] = $signature_id;
216
        $body = json_encode($params);
217
        $url = $this->baseURL . 'template/' . $id;
218
        return $this->PUT($url, $body);
219
    }
220
221
    /**
222
     * 删除模板
223
     *
224
     * @param string $template_id 模板 ID
225
     * @return array
226
     * @link https://developer.qiniu.com/sms/api/5896/sms-api-delete-template
227
     */
228
    public function deleteTemplate($template_id)
229
    {
230
        $url = $this->baseURL . 'template/' . $template_id;
231
        return $this->delete($url);
232
    }
233
234
    /**
235
     * 发送短信
236
     *
237
     * @param string $template_id 模板 ID
238
     * @param array $mobiles 手机号
239
     * @param array $parameters 自定义模板变量,变量设置在创建模板时,参数template指定
240
     * @return array
241
     * @link https://developer.qiniu.com/sms/api/5897/sms-api-send-message
242
     */
243
    public function sendMessage($template_id, $mobiles, $parameters = null)
244
    {
245
        $params = array();
246
        $params['template_id'] = $template_id;
247
        $params['mobiles'] = $mobiles;
248
        if (!empty($parameters)) {
249
            $params['parameters'] = $parameters;
250
        }
251
        $body = json_encode($params);
252
        $url = $this->baseURL . 'message';
253
        return $this->post($url, $body);
254
    }
255
256
    /**
257
     * 查询发送记录
258
     *
259
     * @param string $job_id 发送任务返回的 id
260
     * @param string $message_id 单条短信发送接口返回的 id
261
     * @param string $mobile 接收短信的手机号码
262
     * @param string $status sending: 发送中,success: 发送成功,failed: 发送失败,waiting: 等待发送
263
     * @param string $template_id 模版 id
264
     * @param string $type marketing:营销,notification:通知,verification:验证码,voice:语音
265
     * @param string $start 开始时间,timestamp,例如: 1563280448
266
     * @param int $end 结束时间,timestamp,例如: 1563280471
267
     * @param int $page 页码,默认为 1
268
     * @param int $page_size 每页返回的数据条数,默认20,最大200
269
     * @return array
270
     * @link https://developer.qiniu.com/sms/api/5852/query-send-sms
271
     */
272
    public function querySendSms(
273
        $job_id = null,
274
        $message_id = null,
275
        $mobile = null,
276
        $status = null,
277
        $template_id = null,
278
        $type = null,
279
        $start = null,
280
        $end = null,
281
        $page = 1,
282
        $page_size = 20
283
    ) {
284
        $query = array();
285
        \Qiniu\setWithoutEmpty($query, 'job_id', $job_id);
286
        \Qiniu\setWithoutEmpty($query, 'message_id', $message_id);
287
        \Qiniu\setWithoutEmpty($query, 'mobile', $mobile);
288
        \Qiniu\setWithoutEmpty($query, 'status', $status);
289
        \Qiniu\setWithoutEmpty($query, 'template_id', $template_id);
290
        \Qiniu\setWithoutEmpty($query, 'type', $type);
291
        \Qiniu\setWithoutEmpty($query, 'start', $start);
292
        \Qiniu\setWithoutEmpty($query, 'end', $end);
293
        \Qiniu\setWithoutEmpty($query, 'page', $page);
294
        \Qiniu\setWithoutEmpty($query, 'page_size', $page_size);
295
296
        $url = $this->baseURL . 'messages?' . http_build_query($query);
297
        return $this->get($url);
298
    }
299
300
301
    public function imgToBase64($img_file)
302
    {
303
        $img_base64 = '';
304
        if (file_exists($img_file)) {
305
            $app_img_file = $img_file; // 图片路径
306
            $img_info = getimagesize($app_img_file); // 取得图片的大小,类型等
307
            $fp = fopen($app_img_file, "r"); // 图片是否可读权限
308
            if ($fp) {
309
                $filesize = filesize($app_img_file);
310
                if ($filesize > 5 * 1024 * 1024) {
311
                    die("pic size < 5M !");
312
                }
313
                $img_type = null;
314
                $content = fread($fp, $filesize);
315
                $file_content = chunk_split(base64_encode($content)); // base64编码
316
                switch ($img_info[2]) {           //判读图片类型
317
                    case 1:
318
                        $img_type = 'gif';
319
                        break;
320
                    case 2:
321
                        $img_type = 'jpg';
322
                        break;
323
                    case 3:
324
                        $img_type = 'png';
325
                        break;
326
                }
327
                //合成图片的base64编码
328
                $img_base64 = 'data:image/' . $img_type . ';base64,' . $file_content;
329
            }
330
            fclose($fp);
331
        }
332
333
        return $img_base64;
334
    }
335
336
    private function get($url, $contentType = 'application/x-www-form-urlencoded')
337
    {
338
        $headers = $this->auth->authorizationV2($url, "GET", null, $contentType);
339
        $headers['Content-Type'] = $contentType;
340
        $ret = Client::get($url, $headers, $this->proxy->makeReqOpt());
341
        if (!$ret->ok()) {
342
            return array(null, new Error($url, $ret));
343
        }
344
        return array($ret->json(), null);
345
    }
346
347
    private function delete($url, $contentType = 'application/json')
348
    {
349
        $headers = $this->auth->authorizationV2($url, "DELETE", null, $contentType);
350
        $headers['Content-Type'] = $contentType;
351
        $ret = Client::delete($url, $headers, $this->proxy->makeReqOpt());
0 ignored issues
show
$this->proxy->makeReqOpt() of type Qiniu\Http\RequestOptions is incompatible with the type array expected by parameter $opt of Qiniu\Http\Client::delete(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

351
        $ret = Client::delete($url, $headers, /** @scrutinizer ignore-type */ $this->proxy->makeReqOpt());
Loading history...
352
        if (!$ret->ok()) {
353
            return array(null, new Error($url, $ret));
354
        }
355
        return array($ret->json(), null);
356
    }
357
358
    private function post($url, $body, $contentType = 'application/json')
359
    {
360
        $headers = $this->auth->authorizationV2($url, "POST", $body, $contentType);
361
362
        $headers['Content-Type'] = $contentType;
363
        $ret = Client::post($url, $body, $headers, $this->proxy->makeReqOpt());
364
        if (!$ret->ok()) {
365
            return array(null, new Error($url, $ret));
366
        }
367
        $r = ($ret->body === null) ? array() : $ret->json();
368
        return array($r, null);
369
    }
370
371
    private function PUT($url, $body, $contentType = 'application/json')
372
    {
373
        $headers = $this->auth->authorizationV2($url, "PUT", $body, $contentType);
374
        $headers['Content-Type'] = $contentType;
375
        $ret = Client::put($url, $body, $headers, $this->proxy->makeReqOpt());
376
        if (!$ret->ok()) {
377
            return array(null, new Error($url, $ret));
378
        }
379
        $r = ($ret->body === null) ? array() : $ret->json();
380
        return array($r, null);
381
    }
382
}
383