Skip to content

Commit 7a01c44

Browse files
Add IP2Location.io web service.
1 parent 73b3da0 commit 7a01c44

2 files changed

Lines changed: 78 additions & 23 deletions

File tree

Libraries/IP2Location_lib.php

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,29 @@
66
}
77

88
// Web Service Settings
9-
if(!defined('IP2LOCATION_API_KEY')) {
10-
define('IP2LOCATION_API_KEY', 'demo');
11-
}
9+
if(defined('IP2LOCATION_IO_API_KEY')) {
10+
define('USE_IO', true);
11+
} else {
12+
define('USE_IO', false);
13+
if(!defined('IP2LOCATION_API_KEY')) {
14+
define('IP2LOCATION_API_KEY', 'demo');
15+
}
1216

13-
if(!defined('IP2LOCATION_PACKAGE')) {
14-
define('IP2LOCATION_PACKAGE', 'WS1');
15-
}
17+
if(!defined('IP2LOCATION_PACKAGE')) {
18+
define('IP2LOCATION_PACKAGE', 'WS1');
19+
}
1620

17-
if(!defined('IP2LOCATION_USESSL')) {
18-
define('IP2LOCATION_USESSL', false);
19-
}
21+
if(!defined('IP2LOCATION_USESSL')) {
22+
define('IP2LOCATION_USESSL', false);
23+
}
2024

21-
if(!defined('IP2LOCATION_ADDONS')) {
22-
define('IP2LOCATION_ADDONS', []);
23-
}
25+
if(!defined('IP2LOCATION_ADDONS')) {
26+
define('IP2LOCATION_ADDONS', []);
27+
}
2428

25-
if(!defined('IP2LOCATION_LANGUAGE')) {
26-
define('IP2LOCATION_LANGUAGE', 'en');
29+
if(!defined('IP2LOCATION_LANGUAGE')) {
30+
define('IP2LOCATION_LANGUAGE', 'en');
31+
}
2732
}
2833

2934
require_once('IP2Location/Country.php');
@@ -35,6 +40,8 @@ class IP2Location_lib {
3540
private $database;
3641

3742
protected static $ip2location;
43+
44+
// global $use_io;
3845

3946
public function __construct() {
4047
self::$ip2location = new \IP2Location\Database(IP2LOCATION_DATABASE, \IP2Location\Database::FILE_IO);
@@ -128,8 +135,44 @@ public function getCategory($ip=NULL) {
128135
}
129136

130137
public function getWebService($ip=NULL) {
131-
$ws = new \IP2Location\WebService(IP2LOCATION_API_KEY, IP2LOCATION_PACKAGE, IP2LOCATION_USESSL);
132-
return $ws->lookup(self::getIP($ip), IP2LOCATION_ADDONS, IP2LOCATION_LANGUAGE);
138+
if (USE_IO) {
139+
// Using IP2Location.io API
140+
$ioapi_baseurl = 'https://api.ip2location.io/?';
141+
$params = [
142+
'key' => IP2LOCATION_IO_API_KEY,
143+
'ip' => self::getIP($ip),
144+
'lang' => ((defined('IP2LOCATION_IO_LANGUAGE')) ? IP2LOCATION_IO_LANGUAGE : ''),
145+
];
146+
// Remove parameters without values
147+
$params = array_filter($params);
148+
$url = $ioapi_baseurl . http_build_query($params);
149+
$ch = curl_init();
150+
curl_setopt($ch, CURLOPT_URL, $url);
151+
curl_setopt($ch, CURLOPT_FAILONERROR, 0);
152+
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
153+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
154+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
155+
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
156+
157+
$response = curl_exec($ch);
158+
159+
if (!curl_errno($ch)) {
160+
if (($data = json_decode($response, true)) === null) {
161+
return false;
162+
}
163+
if (array_key_exists('error', $data)) {
164+
throw new \Exception(__CLASS__ . ': ' . $data['error']['error_message'], $data['error']['error_code']);
165+
}
166+
return $data;
167+
}
168+
169+
curl_close($ch);
170+
171+
return false;
172+
} else {
173+
$ws = new \IP2Location\WebService(IP2LOCATION_API_KEY, IP2LOCATION_PACKAGE, IP2LOCATION_USESSL);
174+
return $ws->lookup(self::getIP($ip), IP2LOCATION_ADDONS, IP2LOCATION_LANGUAGE);
175+
}
133176
}
134177

135178
public function isIpv4($ip=NULL) {

README.md

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ Upload `Controllers`, `Libraries` and `Models` to CodeIgniter `app` folder.
2929
This module is able to query the IP address information from either BIN database or web service. This section will explain how to use this extension to query from BIN database and web service.
3030

3131
Sample codes are given in this project, under **Controllers** folder. After added the following line into the *app\Config\Routes.php* file, you may run the sample code by using <your_domain>/index.php/ip2location_test.
32-
```
32+
```php
3333
$routes->get('ip2location_test', 'IP2Location_test::index');
3434
```
3535

3636
### BIN Database
3737
Use following codes in your application for get geolocation information.
38-
```
38+
```php
3939
// (optional) Define IP2Location database path. By default, the IP2LOCATION_DATABASE is pointed to *app/Libraries/IP2Location/IP2LOCATION-DB.BIN* if you choose not to change the original settings.
4040
define('IP2LOCATION_DATABASE', '/path/to/ip2location/database');
4141

@@ -44,7 +44,7 @@ Use following codes in your application for get geolocation information.
4444
```
4545

4646
Below are the methods supported for BIN data file lookup.
47-
```
47+
```php
4848
$countryCode = $ipl->getCountryCode($ip);
4949
$countryName = $ipl->getCountryName($ip);
5050
$regionName = $ipl->getRegionName($ip);
@@ -71,7 +71,7 @@ Below are the methods supported for BIN data file lookup.
7171

7272
### Web Service
7373
Use following codes in your application for get geolocation information.
74-
```
74+
```php
7575
// (required) Define IP2Location API key.
7676
define('IP2LOCATION_API_KEY', 'your_api_key');
7777

@@ -91,9 +91,21 @@ Use following codes in your application for get geolocation information.
9191
print_r ($ipl->getWebService('8.8.8.8'));
9292
```
9393

94+
To use IP2Location.io API for getting the geolocation information, you can use the following code to do so:
95+
```php
96+
// (required) Define IP2Location.io API key.
97+
define('IP2LOCATION_IO_API_KEY', 'your_api_key');
98+
99+
// (optional) Define Translation information. Refer to https://www.ip2location.io/ip2location-documentation for available languages.
100+
define('IP2LOCATION_IO_LANGUAGE', 'zh-cn');
101+
102+
$ipl = new IP2Location_lib();
103+
print_r ($ipl->getWebService('8.8.8.8'));
104+
```
105+
94106
### MySQL Query
95107
Use following codes in your application for get geolocation information.
96-
```
108+
```php
97109
define('IP2LOCATION_DATABASE_TABLE', 'ip2location_table_name');
98110

99111
$db = model('IP2Location_model', false);
@@ -102,7 +114,7 @@ Use following codes in your application for get geolocation information.
102114

103115
### IPTools
104116
Use following codes in your application for get IPTools class information.
105-
```
117+
```php
106118
$ipl = new IP2Location_lib();
107119
var_dump($ipl->isIpv4('8.8.8.8'));echo '<br>';
108120
var_dump($ipl->isIpv6('2001:4860:4860::8888'));echo '<br>';
@@ -128,7 +140,7 @@ An outdated BIN database was provided in this release for your testing. You are
128140

129141
For the BIN database update, you can just rename the downloaded BIN database to *IP2LOCATION-DB.BIN* and replace the copy in *app/Libraries/IP2Location/* (if you didn't change the default IP2LOCATION_DATABASE constant as described in the Usage section).
130142

131-
You can also sign up for [IP2Location Web Service](https://www.ip2location.com/web-service/ip2location) to get one free API key.
143+
You can also sign up for [IP2Location Web Service](https://www.ip2location.com/web-service/ip2location) or [IP2Location.io IP GEOLOCATION API](https://www.ip2location.io/sign-up) to get one free API key.
132144

133145
## IPv4 BIN vs IPv6 BIN
134146
* Use the IPv4 BIN file if you just need to query IPv4 addresses.

0 commit comments

Comments
 (0)