Completed
Pull Request — master (#15)
by lan tian
05:15
created

JuHeAgent::sendContentSms()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
eloc 1
nc 1
nop 2
1
<?php
2
3
namespace Toplan\PhpSms;
4
5
/**
6
 * Class JuHeAgent
7
 *
8
 * @property string $key
9
 * @property string $times
10
 */
11
class JuHeAgent extends Agent
12
{
13
    public function sendSms($tempId, $to, array $data, $content)
14
    {
15
        $this->sendTemplateSms($tempId, $to, $data);
16
    }
17
18
    public function sendContentSms($to, $content)
19
    {
20
    }
21
22
    public function sendTemplateSms($tempId, $to, array $data)
23
    {
24
        $sendUrl = 'http://v.juhe.cn/sms/send';
25
        $tplValue = '';
26
        foreach ($data as $key => $value) {
27
            if (preg_match('/[#&=]/', $value)) {
28
                $value = urlencode($value);
29
            }
30
            $split = !$tplValue ? '' : '&';
31
            $tplValue .= "$split#$key#=$value";
32
        }
33
        $tplValue = !$tplValue ?: urlencode($tplValue);
34
        $smsConf = [
35
            'key'       => $this->key,
36
            'mobile'    => $to,
37
            'tpl_id'    => $tempId,
38
            'tpl_value' => $tplValue,
39
        ];
40
        $result = $this->curl($sendUrl, $smsConf, true);
41
        $this->genResult($result);
42
    }
43
44
    public function voiceVerify($to, $code)
45
    {
46
        $url = 'http://op.juhe.cn/yuntongxun/voice';
47
        $params = [
48
            'valicode'  => $code,
49
            'to'        => $to,
50
            'playtimes' => $this->times ?: 3,
51
            'key'       => $this->key,
52
        ];
53
        $result = $this->curl($url, $params);
54
        $this->genResult($result);
55
    }
56
57
    public function genResult($result)
58
    {
59
        if ($result['request']) {
60
            $result = json_decode($result['response'], true);
61
            if ($result['error_code'] === '0') {
62
                $this->result['success'] = true;
63
            } else {
64
                $this->result['info'] = $result['reason'] . '|result:' . json_encode($result['result'] ?: '');
65
                $this->result['code'] = $result['error_code'];
66
            }
67
        } else {
68
            $this->result['info'] = '请求失败';
69
        }
70
    }
71
}
72