Completed
Pull Request — master (#13)
by lan tian
03:07
created

Agent::curl()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 26
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 26
ccs 4
cts 4
cp 1
rs 8.5806
cc 4
eloc 21
nc 4
nop 3
crap 4
1
<?php
2
3
namespace Toplan\PhpSms;
4
5
abstract class Agent
6
{
7
    /**
8
     * agent config
9
     *
10
     * @var array
11
     */
12
    protected $config;
13
14
    /**
15
     * sent result info
16
     *
17
     * @var array
18
     */
19
    protected $result = [
20
        'success' => false,
21
        'info'    => '',
22
        'code'    => 0,
23
    ];
24
25
    /**
26
     * construct for create a instance
27
     *
28
     * @param array $config
29
     */
30 18
    public function __construct(array $config = [])
31
    {
32 18
        $this->config = $config;
33 18
    }
34
35
    /**
36
     * sms send process
37
     *
38
     * @param       $tempId
39
     * @param       $to
40
     * @param array $data
41
     * @param       $content
42
     */
43
    abstract public function sendSms($tempId, $to, array $data, $content);
44
45
    /**
46
     * content sms send process
47
     *
48
     * @param $to
49
     * @param $content
50
     */
51
    abstract public function sendContentSms($to, $content);
52
53
    /**
54
     * template sms send process
55
     *
56
     * @param       $tempId
57
     * @param       $to
58
     * @param array $data
59
     */
60
    abstract public function sendTemplateSms($tempId, $to, array $data);
61
62
    /**
63
     * voice verify
64
     *
65
     * @param $to
66
     * @param $code
67
     */
68
    abstract public function voiceVerify($to, $code);
69
70
    /**
71
     * http post request
72
     *
73
     * @codeCoverageIgnore
74
     *
75
     * @param       $url
76
     * @param array $query
77
     * @param       $port
78
     *
79
     * @return mixed
80
     */
81
    public function sockPost($url, $query, $port = 80)
82
    {
83
        $data = '';
84
        $info = parse_url($url);
85
        $fp = fsockopen($info['host'], $port, $errno, $errstr, 30);
86
        if (!$fp) {
87
            return $data;
88
        }
89
        $head = 'POST ' . $info['path'] . " HTTP/1.0\r\n";
90
        $head .= 'Host: ' . $info['host'] . "\r\n";
91
        $head .= 'Referer: http://' . $info['host'] . $info['path'] . "\r\n";
92
        $head .= "Content-type: application/x-www-form-urlencoded\r\n";
93
        $head .= 'Content-Length: ' . strlen(trim($query)) . "\r\n";
94
        $head .= "\r\n";
95
        $head .= trim($query);
96
        $write = fwrite($fp, $head);
0 ignored issues
show
Unused Code introduced by
$write is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
97
        $header = '';
98
        while ($str = trim(fgets($fp, 4096))) {
99
            $header .= $str;
100
        }
101
        while (!feof($fp)) {
102
            $data .= fgets($fp, 4096);
103
        }
104
105
        return $data;
106
    }
107
108
    /**
109
     * cURl
110
     *
111
     * @codeCoverageIgnore
112
     *
113 27
     * @param string   $url    [请求的URL地址]
114
     * @param array    $params [请求的参数]
115 27
     * @param int|bool $isPost [是否采用POST形式]
116
     *
117
     * @return array ['request', 'response']
118
     *               request:是否请求成功
119
     *               response:响应数据
120
     */
121
    public function curl($url, array $params = [], $isPost = false)
122
    {
123
        $request = true;
124
        $ch = curl_init();
125 3
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
126
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22');
127 3
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
128 3
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
129
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
130
        if ($isPost) {
131 3
            curl_setopt($ch, CURLOPT_POST, true);
132
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
133
            curl_setopt($ch, CURLOPT_URL, $url);
134
        } else {
135
            $params = http_build_query($params);
136
            curl_setopt($ch, CURLOPT_URL, $params ? "$url?$params" : $url);
137
        }
138
        $response = curl_exec($ch);
139
        if ($response === false) {
140
            $request = false;
141
            $response = curl_getinfo($ch);
142
        }
143
        curl_close($ch);
144
145
        return compact('request', 'response');
146
    }
147
148
    /**
149
     * get result
150
     *
151
     * @return array
152
     */
153
    public function getResult()
154
    {
155
        return $this->result;
156
    }
157
158
    /**
159
     * overload object attribute
160
     *
161
     * @param $name
162
     *
163
     * @return mixed
164
     */
165
    public function __get($name)
166
    {
167
        if (array_key_exists($name, $this->config)) {
168
            return $this->config["$name"];
169
        }
170
171
        return;
172
    }
173
}
174