|
2 | 2 | namespace IP2LocationCakePHP\Controller; |
3 | 3 |
|
4 | 4 | // 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 | + } |
23 | 28 | } |
24 | 29 |
|
25 | 30 | /** |
@@ -56,15 +61,51 @@ public function get($ip, $db = '') |
56 | 61 |
|
57 | 62 | public function getWebService($ip) |
58 | 63 | { |
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 | + } |
68 | 109 | } |
69 | 110 |
|
70 | 111 | public function isIpv4($ip) |
|
0 commit comments