Skip to content

Commit

Permalink
Add support for IP2Location.io API.
Browse files Browse the repository at this point in the history
  • Loading branch information
ip2location committed Nov 30, 2023
1 parent 5b5249b commit 37b55d8
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 7 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,16 @@ Route::get('test', 'TestController@lookup');

### WEB SERVICE

1. To use IP2Proxy Web Service, create a new file called "site_vars.php" in `config` directory.
2. In the site_vars.php, save the following contents:
1. To use IP2Location.io or IP2Proxy Web Service, create a new file called "site_vars.php" in `config` directory.
2. In the site_vars.php, save the following contents for IP2Location.io:
```
<?php
return [
'IP2LocationioAPIKey' => 'your_api_key', // Required. Your IP2Location.io API key.
'IP2LocationioLanguage' => 'en', // Optional. Refer to https://www.ip2location.io/ip2location-documentation for available languages.
];
```
Or save the following contents for IP2Proxy:
```php
<?php
return [
Expand Down
58 changes: 53 additions & 5 deletions src/IP2ProxyLaravel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,23 @@

class IP2ProxyLaravel
{
private $db;
private $ws;

private function load($mode)
{
if ($mode == 'bin')
{
$this->db = new \IP2Proxy\Database($this->getDatabasePath(), \IP2PROXY\Database::FILE_IO);
} else if ($mode == 'ws')
{
$apikey = \Config::get('site_vars.IP2ProxyAPIKey');
$package = (null !== \Config::get('site_vars.IP2ProxyPackage')) ? \Config::get('site_vars.IP2ProxyPackage') : 'PX1';
$ssl = (null !== \Config::get('site_vars.IP2ProxyUsessl')) ? \Config::get('site_vars.IP2ProxyUsessl') : false;
$this->ws = new \IP2Proxy\WebService($apikey, $package, $ssl);
if (!config()->has('site_vars.IP2LocationioAPIKey'))
{
$apikey = \Config::get('site_vars.IP2ProxyAPIKey');
$package = (null !== \Config::get('site_vars.IP2ProxyPackage')) ? \Config::get('site_vars.IP2ProxyPackage') : 'PX1';
$ssl = (null !== \Config::get('site_vars.IP2ProxyUsessl')) ? \Config::get('site_vars.IP2ProxyUsessl') : false;
$this->ws = new \IP2Proxy\WebService($apikey, $package, $ssl);
}
}
}

Expand All @@ -31,7 +37,49 @@ public function get($ip, $mode = 'bin')
$records = $this->db->lookup($ip, \IP2PROXY\Database::ALL);
} else if ($mode == 'ws')
{
$records = $this->ws->lookup($ip);
if (config()->has('site_vars.IP2LocationioAPIKey'))
{
// Use IP2Location.io API
$ioapi_baseurl = 'https://api.ip2location.io/?';
$params = [
'key' => \Config::get('site_vars.IP2LocationioAPIKey'),
'ip' => $ip,
'lang' => ((config()->has('site_vars.IP2LocationioLanguage')) ? \Config::get('site_vars.IP2LocationioLanguage') : ''),
];
// 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);

$response = curl_exec($ch);

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
{
$records = $this->ws->lookup($ip);
}
}
return $records;
}
Expand Down

0 comments on commit 37b55d8

Please sign in to comment.