IpTaobaoApiQuery::setOutTime()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 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 9
    public function query($ip)
19
    {
20 9
        $this->ip = $ip;
21 9
        $result   = $this->doGet(self::URI . $this->ip);
22 9
        if ($result !== false) {
23 9
            $data = json_decode($result, 1);
24 9
            if ($data['code'] !== 1) {
25 9
                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 9
    public function setOutTime($outTime)
40
    {
41 9
        $this->outTime = $outTime;
42
43 9
        return $this;
44
    }
45
46 9
    private function doGet($url)
47
    {
48 9
        $ch = curl_init();
49 9
        curl_setopt($ch, CURLOPT_URL, $url);
50 9
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
51 9
        curl_setopt($ch, CURLOPT_HEADER, 0);
52 9
        curl_setopt($ch, CURLOPT_TIMEOUT_MS, $this->outTime * 1000);
53 9
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
54 9
            "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 9
        ]);
56 9
        $output = curl_exec($ch);
57 9
        curl_close($ch);
58
59 9
        return $output;
60
    }
61
}