Test Failed
Push — master ( 1394e6...2e6134 )
by Feyman
02:46
created

IpTaobaoApiQuery::query()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
1
<?php
2
3
namespace IpInfo;
4
5
class IpTaobaoApiQuery extends IpQueryContract
6
{
7
8
    /**
9
     * 淘宝 IP 库 URL
10
     */
11
    const URI = 'http://ip.taobao.com/service/getIpInfo.php?ip=';
12
    /**
13
     * @var int 超时时间
14
     */
15
    private $outTime = 1;
16
17
18
    public function query($ip)
19
    {
20
        $this->ip = $ip;
21
        $result   = $this->doGet(self::URI . $this->ip);
22
        if ($result !== false) {
23
            $data = json_decode($result, 1);
24
            if ($data['code'] !== 1) {
25
                return $data['data'];
26
            }
27
        }
28
29
        return false;
30
    }
31
32
    /**
33
     * 设置超时时间,单位:s
34
     *
35
     * @param int $outTime
36
     *
37
     * @return $this
38
     */
39
    public function setOutTime(int $outTime)
40
    {
41
        $this->outTime = $outTime;
42
43
        return $this;
44
    }
45
46
    private function doGet($url)
47
    {
48
        $ch = curl_init();
49
        curl_setopt($ch, CURLOPT_URL, $url);
50
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
51
        curl_setopt($ch, CURLOPT_HEADER, 0);
52
        curl_setopt($ch, CURLOPT_TIMEOUT_MS, $this->outTime * 1000);
53
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
54
            "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6 (.NET CLR 3.5.30729)",
55
        ]);
56
        $output = curl_exec($ch);
57
        curl_close($ch);
58
59
        return $output;
60
    }
61
}