Skip to content

Commit

Permalink
Add IP2Location.io API.
Browse files Browse the repository at this point in the history
  • Loading branch information
ip2location committed Dec 11, 2023
1 parent c842b11 commit 81c6e41
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 23 deletions.
13 changes: 5 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,11 @@ namespace App\Controller;
use App\Controller\AppController;
use IP2ProxyCakePHP\Controller\IP2ProxyCoresController;
// (required) Define IP2Proxy API key.
define('IP2PROXY_API_KEY', 'your_api_key');
// (required) Define IP2Location.io API key.
define('IP2LOCATION_IO_API_KEY', 'your_api_key');
// (required) Define IP2Proxy Web service package of different granularity of return information.
define('IP2PROXY_PACKAGE', 'PX1');
// (optional) Define to use https or http.
define('IP2PROXY_USESSL', false);
// (optional) Define Translation information. Refer to https://www.ip2location.io/ip2location-documentation for available languages.
define('IP2LOCATION_IO_LANGUAGE', 'en');
/**
* Tests Controller
Expand Down Expand Up @@ -99,7 +96,7 @@ This library requires IP2Proxy BIN or IP2Proxy API key data file to function. Yo
* IP2Proxy LITE BIN Data (Free): https://lite.ip2location.com
* IP2Proxy Commercial BIN Data (Comprehensive): https://www.ip2location.com/proxy-database

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


## SUPPORT
Expand Down
73 changes: 58 additions & 15 deletions src/Controller/IP2ProxyCoresController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@
namespace IP2ProxyCakePHP\Controller;

// Web Service Settings
if(!defined('IP2PROXY_API_KEY')) {
define('IP2PROXY_API_KEY', 'demo');
}
if((defined('IP2LOCATION_IO_API_KEY')) && (!defined('USE_IO'))) {
define('USE_IO', true);
} else {
if (!defined('USE_IO')) {
define('USE_IO', false);
}
if(!defined('IP2PROXY_API_KEY')) {
define('IP2PROXY_API_KEY', 'demo');
}

if(!defined('IP2PROXY_PACKAGE')) {
define('IP2PROXY_PACKAGE', 'PX1');
}
if(!defined('IP2PROXY_PACKAGE')) {
define('IP2PROXY_PACKAGE', 'PX1');
}

if(!defined('IP2PROXY_USESSL')) {
define('IP2PROXY_USESSL', false);
if(!defined('IP2PROXY_USESSL')) {
define('IP2PROXY_USESSL', false);
}
}

/**
Expand Down Expand Up @@ -50,15 +57,51 @@ public function get($ip, $db = '')

public function getWebService($ip)
{
$ws = new \IP2Proxy\WebService(IP2PROXY_API_KEY, IP2PROXY_PACKAGE, IP2PROXY_USESSL);
if (USE_IO) {
// Using IP2Location.io API
$ioapi_baseurl = 'https://api.ip2location.io/?';
$params = [
'key' => IP2LOCATION_IO_API_KEY,
'ip' => $ip,
'lang' => ((defined('IP2LOCATION_IO_LANGUAGE')) ? IP2LOCATION_IO_LANGUAGE : ''),
];
// Remove parameters without values
$params = array_filter($params);
$url = $ioapi_baseurl . http_build_query($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, 0);
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);

try {
$records = $ws->lookup($ip);
} catch (Exception $e) {
return null;
}
$response = curl_exec($ch);

return $records;
if (!curl_errno($ch)) {
if (($data = json_decode($response, true)) === null) {
return false;
}
if (array_key_exists('error', $data)) {
throw new \Exception(__CLASS__ . ': ' . $data['error']['error_message'], $data['error']['error_code']);
}
return $data;
}

curl_close($ch);

return false;
} else {
$ws = new \IP2Proxy\WebService(IP2PROXY_API_KEY, IP2PROXY_PACKAGE, IP2PROXY_USESSL);

try {
$records = $ws->lookup($ip);
} catch (Exception $e) {
return null;
}

return $records;
}
}

}

0 comments on commit 81c6e41

Please sign in to comment.