Completed
Pull Request — master (#62)
by lan tian
02:30
created

LuosimaoAgent::sendContentSms()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 10
rs 9.4285
cc 1
eloc 7
nc 1
nop 2
1
<?php
2
3
namespace Toplan\PhpSms;
4
5
/**
6
 * Class LuosimaoAgent
7
 *
8
 * @property string $apikey
9
 * @property string $voiceApikey
10
 */
11
class LuosimaoAgent extends Agent
12
{
13
    public function sendSms($to, $content, $tempId, array $data)
14
    {
15
        // check content signature,
16
        // Luosimao signature must be in the content finally
17
        if ($content && !preg_match('/】$/', $content)) {
18
            preg_match('/【([0-9a-zA-Z\W]+)】/', $content, $matches);
19
            if (isset($matches[0])) {
20
                $content = str_replace($matches[0], '', $content) . $matches[0];
21
            }
22
        }
23
        $this->sendContentSms($to, $content);
24
    }
25
26
    public function sendContentSms($to, $content)
27
    {
28
        $url = 'https://sms-api.luosimao.com/v1/send.json';
29
        $optData = [
30
            'mobile'  => $to,
31
            'message' => $content,
32
        ];
33
        $data = $this->LuosimaoCurl($url, $optData, $this->apikey);
34
        $this->setResult($data);
35
    }
36
37
    public function voiceVerify($to, $code, $tempId, array $data)
38
    {
39
        $url = 'https://voice-api.luosimao.com/v1/verify.json';
40
        $optData = [
41
            'mobile' => $to,
42
            'code'   => $code,
43
        ];
44
        $data = $this->LuosimaoCurl($url, $optData, $this->voiceApikey);
45
        $this->setResult($data);
46
    }
47
48
    protected function LuosimaoCurl($url, $optData, $apikey)
49
    {
50
        $ch = curl_init();
51
        curl_setopt($ch, CURLOPT_URL, "$url");
52
53
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
54
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
55
        curl_setopt($ch, CURLOPT_HEADER, false);
56
57
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
58
        curl_setopt($ch, CURLOPT_USERPWD, 'api:key-' . $apikey);
59
60
        curl_setopt($ch, CURLOPT_POST, true);
61
        curl_setopt($ch, CURLOPT_POSTFIELDS, $optData);
62
63
        $res = curl_exec($ch);
64
        curl_close($ch);
65
66
        return json_decode($res, true);
67
    }
68
69
    protected function setResult($result)
70
    {
71
        $this->result(Agent::SUCCESS, $result['error'] === 0);
72
        $this->result(Agent::INFO, $result['msg']);
73
        $this->result(Agent::CODE, $result['error']);
74
    }
75
76
    public function sendTemplateSms($to, $tempId, array $data)
77
    {
78
    }
79
}
80