Skip to content

Commit 37b55d8

Browse files
committed
Add support for IP2Location.io API.
1 parent 5b5249b commit 37b55d8

File tree

2 files changed

+63
-7
lines changed

2 files changed

+63
-7
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,16 @@ Route::get('test', 'TestController@lookup');
7272

7373
### WEB SERVICE
7474

75-
1. To use IP2Proxy Web Service, create a new file called "site_vars.php" in `config` directory.
76-
2. In the site_vars.php, save the following contents:
75+
1. To use IP2Location.io or IP2Proxy Web Service, create a new file called "site_vars.php" in `config` directory.
76+
2. In the site_vars.php, save the following contents for IP2Location.io:
77+
```
78+
<?php
79+
return [
80+
'IP2LocationioAPIKey' => 'your_api_key', // Required. Your IP2Location.io API key.
81+
'IP2LocationioLanguage' => 'en', // Optional. Refer to https://www.ip2location.io/ip2location-documentation for available languages.
82+
];
83+
```
84+
Or save the following contents for IP2Proxy:
7785
```php
7886
<?php
7987
return [

src/IP2ProxyLaravel.php

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,23 @@
44

55
class IP2ProxyLaravel
66
{
7+
private $db;
8+
private $ws;
9+
710
private function load($mode)
811
{
912
if ($mode == 'bin')
1013
{
1114
$this->db = new \IP2Proxy\Database($this->getDatabasePath(), \IP2PROXY\Database::FILE_IO);
1215
} else if ($mode == 'ws')
1316
{
14-
$apikey = \Config::get('site_vars.IP2ProxyAPIKey');
15-
$package = (null !== \Config::get('site_vars.IP2ProxyPackage')) ? \Config::get('site_vars.IP2ProxyPackage') : 'PX1';
16-
$ssl = (null !== \Config::get('site_vars.IP2ProxyUsessl')) ? \Config::get('site_vars.IP2ProxyUsessl') : false;
17-
$this->ws = new \IP2Proxy\WebService($apikey, $package, $ssl);
17+
if (!config()->has('site_vars.IP2LocationioAPIKey'))
18+
{
19+
$apikey = \Config::get('site_vars.IP2ProxyAPIKey');
20+
$package = (null !== \Config::get('site_vars.IP2ProxyPackage')) ? \Config::get('site_vars.IP2ProxyPackage') : 'PX1';
21+
$ssl = (null !== \Config::get('site_vars.IP2ProxyUsessl')) ? \Config::get('site_vars.IP2ProxyUsessl') : false;
22+
$this->ws = new \IP2Proxy\WebService($apikey, $package, $ssl);
23+
}
1824
}
1925
}
2026

@@ -31,7 +37,49 @@ public function get($ip, $mode = 'bin')
3137
$records = $this->db->lookup($ip, \IP2PROXY\Database::ALL);
3238
} else if ($mode == 'ws')
3339
{
34-
$records = $this->ws->lookup($ip);
40+
if (config()->has('site_vars.IP2LocationioAPIKey'))
41+
{
42+
// Use IP2Location.io API
43+
$ioapi_baseurl = 'https://api.ip2location.io/?';
44+
$params = [
45+
'key' => \Config::get('site_vars.IP2LocationioAPIKey'),
46+
'ip' => $ip,
47+
'lang' => ((config()->has('site_vars.IP2LocationioLanguage')) ? \Config::get('site_vars.IP2LocationioLanguage') : ''),
48+
];
49+
// Remove parameters without values
50+
$params = array_filter($params);
51+
$url = $ioapi_baseurl . http_build_query($params);
52+
$ch = curl_init();
53+
curl_setopt($ch, CURLOPT_URL, $url);
54+
curl_setopt($ch, CURLOPT_FAILONERROR, 0);
55+
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
56+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
57+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
58+
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
59+
60+
$response = curl_exec($ch);
61+
62+
if (!curl_errno($ch))
63+
{
64+
if (($data = json_decode($response, true)) === null)
65+
{
66+
return false;
67+
}
68+
if (array_key_exists('error', $data))
69+
{
70+
throw new \Exception(__CLASS__ . ': ' . $data['error']['error_message'], $data['error']['error_code']);
71+
}
72+
return $data;
73+
}
74+
75+
curl_close($ch);
76+
77+
return false;
78+
79+
} else
80+
{
81+
$records = $this->ws->lookup($ip);
82+
}
3583
}
3684
return $records;
3785
}

0 commit comments

Comments
 (0)