-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathWebService.php
147 lines (124 loc) · 3.28 KB
/
WebService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
namespace IP2Location;
/**
* IP2Location web service class.
*/
class WebService
{
/**
* No cURL extension found.
*
* @var int
*/
public const EXCEPTION_NO_CURL = 10001;
/**
* Invalid API key format.
*
* @var int
*/
public const EXCEPTION_INVALID_API_KEY = 10002;
/**
* Web service error.
*
* @var int
*/
public const EXCEPTION_WEB_SERVICE_ERROR = 10003;
private $apiKey;
private $package;
private $useSsl;
/**
* Constructor.
*
* @param string $apiKey API key of your IP2Location web service
* @param string $package Supported IP2Location package from WS1 to WS24
* @param bool $useSsl Enable or disabled HTTPS connection. HTTP is faster but less secure.
*
* @throws \Exception
*/
public function __construct($apiKey, $package = 'WS1', $useSsl = false)
{
if (!\extension_loaded('curl')) {
throw new \Exception(__CLASS__ . ": Please make sure your PHP setup has the 'curl' extension enabled.", self::EXCEPTION_NO_CURL);
}
if (!preg_match('/^[0-9A-Z]{10}$/', $apiKey) && $apiKey != 'demo') {
throw new \Exception(__CLASS__ . ': Please provide a valid IP2Location web service API key.', self::EXCEPTION_INVALID_API_KEY);
}
if (!preg_match('/^WS[0-9]+$/', $package)) {
$package = 'WS1';
}
$this->apiKey = $apiKey;
$this->package = $package;
$this->useSsl = $useSsl;
}
/**
* This function will look the given IP address up in IP2Location web service.
*
* @param string $ip IP address to look up
* @param array $addOns Extra fields to return. Please refer to https://www.ip2location.com/web-service/ip2location
* @param string $language the translation for continent, country, region and city name for the addon package
*
* @throws \Exception
*
* @return array|false
*/
public function lookup($ip, $addOns = [], $language = 'en')
{
$response = $this->httpRequest('http://api.ip2location.com/v2/?' . http_build_query([
'key' => $this->apiKey,
'ip' => $ip,
'package' => $this->package,
'addon' => implode(',', $addOns),
'lang' => $language,
]));
if (($data = json_decode($response, true)) === null) {
return false;
}
if ($data['response'] != 'OK') {
throw new \Exception(__CLASS__ . ': ' . $data['response'], self::EXCEPTION_WEB_SERVICE_ERROR);
}
return $data;
}
/**
* Get the remaing credit in your IP2Location web service account.
*
* @return int
*/
public function getCredit()
{
$response = $this->httpRequest('http://api.ip2location.com/v2/?' . http_build_query([
'key' => $this->apiKey,
'check' => true,
]));
if (($data = json_decode($response, true)) === null) {
return 0;
}
if (!isset($data['response'])) {
return 0;
}
return $data['response'];
}
/**
* Open a remote web address.
*
* @param string $url Website URL
*
* @return bool|string
*/
private function httpRequest($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$response = curl_exec($ch);
if (!curl_errno($ch)) {
curl_close($ch);
return $response;
}
curl_close($ch);
return false;
}
}