Completed
Push — master ( df261c...47246b )
by lan tian
02:21
created

Ucpaas::getClientList()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 18

Duplication

Lines 25
Ratio 100 %
Metric Value
dl 25
loc 25
rs 8.8571
cc 3
eloc 18
nc 3
nop 4
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
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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 $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 $url
80
     * @param string $type
81
     *
82
     * @return mixed|string
83
     */
84
    private function getResult($url, $body = null, $type = 'json', $method)
85
    {
86
        $data = $this->connection($url, $body, $type, $method);
87
        if (isset($data) && !empty($data)) {
88
            $result = $data;
89
        } else {
90
            $result = '没有返回数据';
91
        }
92
93
        return $result;
94
    }
95
96
    /**
97
     * @param $url
98
     * @param $type
99
     * @param $body  post数据
100
     * @param $method post或get
101
     *
102
     * @return mixed|string
103
     */
104
    private function connection($url, $body, $type, $method)
105
    {
106
        if ($type === 'json') {
107
            $mine = 'application/json';
108
        } else {
109
            $mine = 'application/xml';
110
        }
111
        if (function_exists('curl_init')) {
112
            $header = array(
113
                'Accept:' . $mine,
114
                'Content-Type:' . $mine . ';charset=utf-8',
115
                'Authorization:' . $this->getAuthorization(),
116
            );
117
            $ch = curl_init($url);
118
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
119
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
120
            if ($method === 'post') {
121
                curl_setopt($ch, CURLOPT_POST, 1);
122
                curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
123
            }
124
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
125
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
126
            $result = curl_exec($ch);
127
            curl_close($ch);
128
        } else {
129
            $opts = array();
130
            $opts['http'] = array();
131
            $headers = array(
132
                'method' => strtoupper($method),
133
            );
134
            $headers[] = 'Accept:' . $mine;
135
            $headers['header'] = array();
136
            $headers['header'][] = 'Authorization: ' . $this->getAuthorization();
137
            $headers['header'][] = 'Content-Type:' . $mine . ';charset=utf-8';
138
139
            if (!empty($body)) {
140
                $headers['header'][] = 'Content-Length:' . strlen($body);
141
                $headers['content'] = $body;
142
            }
143
144
            $opts['http'] = $headers;
145
            $result = file_get_contents($url, false, stream_context_create($opts));
146
        }
147
148
        return $result;
149
    }
150
151
    /**
152
     * @param string $type 默认json,也可指定xml,否则抛出异常
153
     *
154
     * @throws Exception
155
     *
156
     * @return mixed|string 返回指定$type格式的数据
157
     * @links http://www.ucpaas.com/page/doc/doc_rest2-1.jsp
158
     */
159
    public function getDevinfo($type = 'json')
160
    {
161
        if ($type === 'json') {
162
            $type = 'json';
163
        } elseif ($type === 'xml') {
164
            $type = 'xml';
165
        } else {
166
            throw new Exception('只能json或xml,默认为json');
167
        }
168
        $url = self::BaseUrl . self::SoftVersion . '/Accounts/' . $this->accountSid . '?sig=' . $this->getSigParameter();
169
        $data = $this->getResult($url, null, $type, 'get');
170
171
        return $data;
172
    }
173
174
    /**
175
     * @param $appId 应用ID
176
     * @param $clientType 计费方式。0  开发者计费;1 云平台计费。默认为0.
177
     * @param $charge 充值的金额
178
     * @param $friendlyName 昵称
179
     * @param $mobile 手机号码
180
     *
181
     * @return json/xml
0 ignored issues
show
Documentation introduced by
The doc-type json/xml could not be parsed: Unknown type name "json/xml" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
182
     * @links http://www.ucpaas.com/page/doc/doc_rest2-2.jsp
183
     */
184
    public function applyClient($appId, $clientType, $charge, $friendlyName, $mobile, $type = 'json')
185
    {
186
        $url = self::BaseUrl . self::SoftVersion . '/Accounts/' . $this->accountSid . '/Clients?sig=' . $this->getSigParameter();
187
        if ($type === 'json') {
188
            $body_json = array();
189
            $body_json['client'] = array();
190
            $body_json['client']['appId'] = $appId;
191
            $body_json['client']['clientType'] = $clientType;
192
            $body_json['client']['charge'] = $charge;
193
            $body_json['client']['friendlyName'] = $friendlyName;
194
            $body_json['client']['mobile'] = $mobile;
195
            $body = json_encode($body_json);
196
        } elseif ($type === 'xml') {
197
            $body_xml = '<?xml version="1.0" encoding="utf-8"?>
198
                        <client><appId>' . $appId . '</appId>
199
                        <clientType>' . $clientType . '</clientType>
200
                        <charge>' . $charge . '</charge>
201
                        <friendlyName>' . $friendlyName . '</friendlyName>
202
                        <mobile>' . $mobile . '</mobile>
203
                        </client>';
204
            $body = trim($body_xml);
205
        } else {
206
            throw new Exception('只能json或xml,默认为json');
207
        }
208
        $data = $this->getResult($url, $body, $type, 'post');
209
210
        return $data;
211
    }
212
213
    /**
214
     * @param $clientNumber
215
     * @param $appId
216
     * @param string $type
217
     *
218
     * @throws Exception
219
     *
220
     * @return mixed|string
221
     * @links http://www.ucpaas.com/page/doc/doc_rest2-3.jsp
222
     */
223
    public function releaseClient($clientNumber, $appId, $type = 'json')
224
    {
225
        $url = self::BaseUrl . self::SoftVersion . '/Accounts/' . $this->accountSid . '/dropClient?sig=' . $this->getSigParameter();
226
        if ($type === 'json') {
227
            $body_json = array();
228
            $body_json['client'] = array();
229
            $body_json['client']['clientNumber'] = $clientNumber;
230
            $body_json['client']['appId'] = $appId;
231
            $body = json_encode($body_json);
232 View Code Duplication
        } elseif ($type === 'xml') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
233
            $body_xml = '<?xml version="1.0" encoding="utf-8"?>
234
                        <client>
235
                        <clientNumber>' . $clientNumber . '</clientNumber>
236
                        <appId>' . $appId . '</appId >
237
                        </client>';
238
            $body = trim($body_xml);
239
        } else {
240
            throw new Exception('只能json或xml,默认为json');
241
        }
242
        $data = $this->getResult($url, $body, $type, 'post');
243
244
        return $data;
245
    }
246
247
    /**
248
     * @param $appId
249
     * @param $start
250
     * @param $limit
251
     * @param string $type
252
     *
253
     * @throws Exception
254
     *
255
     * @return mixed|string
256
     * @links http://www.ucpaas.com/page/doc/doc_rest2-4.jsp
257
     */
258 View Code Duplication
    public function getClientList($appId, $start, $limit, $type = 'json')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
259
    {
260
        $url = self::BaseUrl . self::SoftVersion . '/Accounts/' . $this->accountSid . '/clientList?sig=' . $this->getSigParameter();
261
        if ($type === 'json') {
262
            $body_json = array('client' => array(
263
                'appId' => $appId,
264
                'start' => $start,
265
                'limit' => $limit,
266
            ));
267
            $body = json_encode($body_json);
268
        } elseif ($type === 'xml') {
269
            $body_xml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
270
                        <client>
271
                            <appId>' . $appId . '</appId>
272
                            <start>' . $start . '</start>
273
                            <limit>' . $limit . '</limit>
274
                        </client>';
275
            $body = trim($body_xml);
276
        } else {
277
            throw new Exception('只能json或xml,默认为json');
278
        }
279
        $data = $this->getResult($url, $body, $type, 'post');
280
281
        return $data;
282
    }
283
284
    /**
285
     * @param $appId
286
     * @param $clientNumber
287
     * @param string $type
288
     *
289
     * @throws Exception
290
     *
291
     * @return mixed|string
292
     * @links http://www.ucpaas.com/page/doc/doc_rest2-5.jsp
293
     */
294 View Code Duplication
    public function getClientInfo($appId, $clientNumber, $type = 'json')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
295
    {
296
        if ($type === 'json') {
297
            $type = 'json';
298
        } elseif ($type === 'xml') {
299
            $type = 'xml';
300
        } else {
301
            throw new Exception('只能json或xml,默认为json');
302
        }
303
        $url = self::BaseUrl . self::SoftVersion . '/Accounts/' . $this->accountSid . '/Clients?sig=' . $this->getSigParameter() . '&clientNumber=' . $clientNumber . '&appId=' . $appId;
304
        $data = $this->getResult($url, null, $type, 'get');
305
306
        return $data;
307
    }
308
309
    /**
310
     * @param $appId
311
     * @param $mobile
312
     * @param string $type
313
     *
314
     * @throws Exception
315
     *
316
     * @return mixed|string
317
     * @links http://www.ucpaas.com/page/doc/doc_rest2-9.jsp
318
     */
319 View Code Duplication
    public function getClientInfoByMobile($appId, $mobile, $type = 'json')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
320
    {
321
        if ($type === 'json') {
322
            $type = 'json';
323
        } elseif ($type === 'xml') {
324
            $type = 'xml';
325
        } else {
326
            throw new Exception('只能json或xml,默认为json');
327
        }
328
        $url = self::BaseUrl . self::SoftVersion . '/Accounts/' . $this->accountSid . '/ClientsByMobile?sig=' . $this->getSigParameter() . '&mobile=' . $mobile . '&appId=' . $appId;
329
        $data = $this->getResult($url, null, $type, 'get');
330
331
        return $data;
332
    }
333
334
    /**
335
     * @param $appId
336
     * @param $date
337
     * @param string $type
338
     *
339
     * @throws Exception
340
     *
341
     * @return mixed|string
342
     * @links http://www.ucpaas.com/page/doc/doc_rest2-6.jsp
343
     */
344
    public function getBillList($appId, $date, $type = 'json')
345
    {
346
        $url = self::BaseUrl . self::SoftVersion . '/Accounts/' . $this->accountSid . '/billList?sig=' . $this->getSigParameter();
347
        if ($type === 'json') {
348
            $body_json = array('appBill' => array(
349
                'appId' => $appId,
350
                'date'  => $date,
351
            ));
352
            $body = json_encode($body_json);
353 View Code Duplication
        } elseif ($type === 'xml') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
354
            $body_xml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
355
                        <appBill>
356
                            <appId>' . $appId . '</appId>
357
                            <date>' . $date . '</date>
358
                        </appBill>';
359
            $body = trim($body_xml);
360
        } else {
361
            throw new Exception('只能json或xml,默认为json');
362
        }
363
        $data = $this->getResult($url, $body, $type, 'post');
364
365
        return $data;
366
    }
367
368
    /**
369
     * @param $appId
370
     * @param $clientNumber
371
     * @param $chargeType
372
     * @param $charge
373
     * @param string $type
374
     *
375
     * @throws Exception
376
     *
377
     * @return mixed|string
378
     * @links http://www.ucpaas.com/page/doc/doc_rest2-8.jsp
379
     */
380 View Code Duplication
    public function chargeClient($appId, $clientNumber, $chargeType, $charge, $type = 'json')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
381
    {
382
        $url = self::BaseUrl . self::SoftVersion . '/Accounts/' . $this->accountSid . '/chargeClient?sig=' . $this->getSigParameter();
383
        if ($type === 'json') {
384
            $body_json = array('client' => array(
385
                'appId'        => $appId,
386
                'clientNumber' => $clientNumber,
387
                'chargeType'   => $chargeType,
388
                'charge'       => $charge,
389
            ));
390
            $body = json_encode($body_json);
391
        } elseif ($type === 'xml') {
392
            $body_xml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
393
                        <client>
394
                            <clientNumber>' . $clientNumber . '</clientNumber>
395
                            <chargeType>' . $chargeType . '</chargeType>
396
                            <charge>' . $charge . '</charge>
397
                            <appId>' . $appId . '</appId>
398
                        </client>';
399
            $body = trim($body_xml);
400
        } else {
401
            throw new Exception('只能json或xml,默认为json');
402
        }
403
        $data = $this->getResult($url, $body, $type, 'post');
404
405
        return $data;
406
    }
407
408
    /**
409
     * @param $appId
410
     * @param $fromClient
411
     * @param $to
412
     * @param null   $fromSerNum
413
     * @param null   $toSerNum
414
     * @param string $type
415
     *
416
     * @throws Exception
417
     *
418
     * @return mixed|string
419
     * @links http://www.ucpaas.com/page/doc/doc_rest3-1.jsp
420
     */
421
    public function callBack($appId, $fromClient, $to, $fromSerNum = null, $toSerNum = null, $type = 'json')
422
    {
423
        $url = self::BaseUrl . self::SoftVersion . '/Accounts/' . $this->accountSid . '/Calls/callBack?sig=' . $this->getSigParameter();
424
        if ($type === 'json') {
425
            $body_json = array('callback' => array(
426
                'appId'      => $appId,
427
                'fromClient' => $fromClient,
428
                'fromSerNum' => $fromSerNum,
429
                'to'         => $to,
430
                'toSerNum'   => $toSerNum,
431
            ));
432
            $body = json_encode($body_json);
433
        } elseif ($type === 'xml') {
434
            $body_xml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
435
                        <callback>
436
                            <fromClient>' . $fromClient . '</clientNumber>
437
                            <fromSerNum>' . $fromSerNum . '</chargeType>
438
                            <to>' . $to . '</charge>
439
                            <toSerNum>' . $toSerNum . '</toSerNum>
440
                            <appId>' . $appId . '</appId>
441
                        </callback>';
442
            $body = trim($body_xml);
443
        } else {
444
            throw new Exception('只能json或xml,默认为json');
445
        }
446
        $data = $this->getResult($url, $body, $type, 'post');
447
448
        return $data;
449
    }
450
451
    /**
452
     * @param $appId
453
     * @param $verifyCode
454
     * @param $to
455
     * @param string $type
456
     *
457
     * @throws Exception
458
     *
459
     * @return mixed|string
460
     * @links http://www.ucpaas.com/page/doc/doc_rest3-2.jsp
461
     */
462 View Code Duplication
    public function voiceCode($appId, $verifyCode, $to, $type = 'json')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
463
    {
464
        $url = self::BaseUrl . self::SoftVersion . '/Accounts/' . $this->accountSid . '/Calls/voiceCode?sig=' . $this->getSigParameter();
465
        if ($type === 'json') {
466
            $body_json = array('voiceCode' => array(
467
                'appId'      => $appId,
468
                'verifyCode' => $verifyCode,
469
                'to'         => $to,
470
            ));
471
            $body = json_encode($body_json);
472
        } elseif ($type === 'xml') {
473
            $body_xml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
474
                        <voiceCode>
475
                            <verifyCode>' . $verifyCode . '</clientNumber>
476
                            <to>' . $to . '</charge>
477
                            <appId>' . $appId . '</appId>
478
                        </voiceCode>';
479
            $body = trim($body_xml);
480
        } else {
481
            throw new Exception('只能json或xml,默认为json');
482
        }
483
        $data = $this->getResult($url, $body, $type, 'post');
484
485
        return $data;
486
    }
487
488
    /**
489
     * @param $appId
490
     * @param $to
491
     * @param $templateId
492
     * @param null   $param
493
     * @param string $type
494
     *
495
     * @throws Exception
496
     *
497
     * @return mixed|string
498
     * @links http://www.ucpaas.com/page/doc/doc_rest4-1.jsp
499
     */
500 View Code Duplication
    public function templateSMS($appId, $to, $templateId, $param = null, $type = 'json')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
501
    {
502
        $url = self::BaseUrl . self::SoftVersion . '/Accounts/' . $this->accountSid . '/Messages/templateSMS?sig=' . $this->getSigParameter();
503
        if ($type === 'json') {
504
            $body_json = array('templateSMS' => array(
505
                'appId'      => $appId,
506
                'templateId' => $templateId,
507
                'to'         => $to,
508
                'param'      => $param,
509
            ));
510
            $body = json_encode($body_json);
511
        } elseif ($type === 'xml') {
512
            $body_xml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
513
                        <templateSMS>
514
                            <templateId>' . $templateId . '</templateId>
515
                            <to>' . $to . '</to>
516
                            <param>' . $param . '</param>
517
                            <appId>' . $appId . '</appId>
518
                        </templateSMS>';
519
            $body = trim($body_xml);
520
        } else {
521
            throw new Exception('只能json或xml,默认为json');
522
        }
523
        $data = $this->getResult($url, $body, $type, 'post');
524
525
        return $data;
526
    }
527
}
528