Skip to content

Commit f4db987

Browse files
committed
Add IP2Location.io API.
1 parent 061f607 commit f4db987

File tree

2 files changed

+73
-41
lines changed

2 files changed

+73
-41
lines changed

README.md

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,11 @@ namespace App\Controller;
4141
use App\Controller\AppController;
4242
use IP2LocationCakePHP\Controller\IP2LocationCoresController;
4343
44-
// (required) Define IP2Location API key.
45-
define('IP2LOCATION_API_KEY', 'your_api_key');
44+
// (required) Define IP2Location.io API key.
45+
define('IP2LOCATION_IO_API_KEY', 'your_api_key');
4646
47-
// (required) Define IP2Location Web service package of different granularity of return information.
48-
define('IP2LOCATION_PACKAGE', 'WS1');
49-
50-
// (optional) Define to use https or http.
51-
define('IP2LOCATION_USESSL', false);
52-
53-
// (optional) Define extra information in addition to the above-selected package. Refer to https://www.ip2location.com/web-service/ip2location for the list of available addons.
54-
define('IP2LOCATION_ADDONS', []);
55-
56-
// (optional) Define Translation information. Refer to https://www.ip2location.com/web-service/ip2location for available languages.
57-
define('IP2LOCATION_LANGUAGE', 'en');
47+
// (optional) Define Translation information. Refer to https://www.ip2location.io/ip2location-documentation for available languages.
48+
define('IP2LOCATION_IO_LANGUAGE', 'en');
5849
5950
/**
6051
* Tests Controller
@@ -128,7 +119,7 @@ This library requires IP2Location BIN data file or IP2Location API key to functi
128119
* IP2Location LITE BIN Data (Free): https://lite.ip2location.com
129120
* IP2Location Commercial BIN Data (Comprehensive): https://www.ip2location.com
130121

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

133124

134125
## IPv4 BIN vs IPv6 BIN

src/Controller/IP2LocationCoresController.php

Lines changed: 68 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,29 @@
22
namespace IP2LocationCakePHP\Controller;
33

44
// Web Service Settings
5-
if(!defined('IP2LOCATION_API_KEY')) {
6-
define('IP2LOCATION_API_KEY', 'demo');
7-
}
8-
9-
if(!defined('IP2LOCATION_PACKAGE')) {
10-
define('IP2LOCATION_PACKAGE', 'WS1');
11-
}
12-
13-
if(!defined('IP2LOCATION_USESSL')) {
14-
define('IP2LOCATION_USESSL', false);
15-
}
16-
17-
if(!defined('IP2LOCATION_ADDONS')) {
18-
define('IP2LOCATION_ADDONS', []);
19-
}
20-
21-
if(!defined('IP2LOCATION_LANGUAGE')) {
22-
define('IP2LOCATION_LANGUAGE', 'en');
5+
if(defined('IP2LOCATION_IO_API_KEY')) {
6+
define('USE_IO', true);
7+
} else {
8+
define('USE_IO', false);
9+
if(!defined('IP2LOCATION_API_KEY')) {
10+
define('IP2LOCATION_API_KEY', 'demo');
11+
}
12+
13+
if(!defined('IP2LOCATION_PACKAGE')) {
14+
define('IP2LOCATION_PACKAGE', 'WS1');
15+
}
16+
17+
if(!defined('IP2LOCATION_USESSL')) {
18+
define('IP2LOCATION_USESSL', false);
19+
}
20+
21+
if(!defined('IP2LOCATION_ADDONS')) {
22+
define('IP2LOCATION_ADDONS', []);
23+
}
24+
25+
if(!defined('IP2LOCATION_LANGUAGE')) {
26+
define('IP2LOCATION_LANGUAGE', 'en');
27+
}
2328
}
2429

2530
/**
@@ -56,15 +61,51 @@ public function get($ip, $db = '')
5661

5762
public function getWebService($ip)
5863
{
59-
$ws = new \IP2Location\WebService(IP2LOCATION_API_KEY, IP2LOCATION_PACKAGE, IP2LOCATION_USESSL);
60-
61-
try {
62-
$records = $ws->lookup($ip, IP2LOCATION_ADDONS, IP2LOCATION_LANGUAGE);
63-
} catch (Exception $e) {
64-
return null;
65-
}
66-
67-
return $records;
64+
if (USE_IO) {
65+
// Using IP2Location.io API
66+
$ioapi_baseurl = 'https://api.ip2location.io/?';
67+
$params = [
68+
'key' => IP2LOCATION_IO_API_KEY,
69+
'ip' => $ip,
70+
'lang' => ((defined('IP2LOCATION_IO_LANGUAGE')) ? IP2LOCATION_IO_LANGUAGE : ''),
71+
];
72+
// Remove parameters without values
73+
$params = array_filter($params);
74+
$url = $ioapi_baseurl . http_build_query($params);
75+
$ch = curl_init();
76+
curl_setopt($ch, CURLOPT_URL, $url);
77+
curl_setopt($ch, CURLOPT_FAILONERROR, 0);
78+
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
79+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
80+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
81+
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
82+
83+
$response = curl_exec($ch);
84+
85+
if (!curl_errno($ch)) {
86+
if (($data = json_decode($response, true)) === null) {
87+
return false;
88+
}
89+
if (array_key_exists('error', $data)) {
90+
throw new \Exception(__CLASS__ . ': ' . $data['error']['error_message'], $data['error']['error_code']);
91+
}
92+
return $data;
93+
}
94+
95+
curl_close($ch);
96+
97+
return false;
98+
} else {
99+
$ws = new \IP2Location\WebService(IP2LOCATION_API_KEY, IP2LOCATION_PACKAGE, IP2LOCATION_USESSL);
100+
101+
try {
102+
$records = $ws->lookup($ip, IP2LOCATION_ADDONS, IP2LOCATION_LANGUAGE);
103+
} catch (Exception $e) {
104+
return null;
105+
}
106+
107+
return $records;
108+
}
68109
}
69110

70111
public function isIpv4($ip)

0 commit comments

Comments
 (0)