Skip to content

Commit 5d0a4b5

Browse files
author
pulkit
committed
added support for ip-api pro
1 parent 6fc010b commit 5d0a4b5

File tree

7 files changed

+101
-18
lines changed

7 files changed

+101
-18
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
sudo: false
12
language: php
23

34
php:

README.md

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,38 @@ Next run `php artisan config:publish pulkitjalan/geoip` to publish the config fi
4444

4545
## Usage
4646

47-
The geoip class takes a config array as the first parameter. Example:
47+
Supported Drivers: [maxmind](https://www.maxmind.com/) and [ip-api](http://ip-api.com/)
48+
49+
The geoip class takes a config array as the first parameter or defaults to using the `ip-api` driver.
50+
51+
Example:
4852

4953
```php
5054
<?php
5155

5256
use PulkitJalan\GeoIP\GeoIP
5357

54-
$config = ['driver' => 'ip-api'];
58+
$geoip = new GeoIP();
59+
$geoip->setIp('81.2.69.160'); // Optional
5560

56-
$geoip = new GeoIP($config);
61+
$lat = $geoip->getLatitude(); // 51.5141
62+
$lon = $geoip->getLongitude(); // -3.1969
5763
```
5864

59-
Supported Drivers: [maxmind](https://www.maxmind.com/) and [ip-api](http://ip-api.com/)
65+
### IP-API
66+
67+
To use the ip-api pro service you can set the options in your config.
68+
69+
```php
70+
$config = [
71+
'driver' => 'ip-api',
72+
'ip-api' => [
73+
'key' => 'YOUR IP-API KEY',
74+
],
75+
];
76+
```
77+
78+
### Maxmind
6079

6180
Maxmind support the database type and also web api type.
6281

@@ -83,9 +102,9 @@ $config = [
83102

84103
### Laravel
85104

86-
Just update the config file in `config/packages/pulkitjalan/geoip/config.php` to get the same effect.
105+
To use this package in Laravel, simply update the config file in `config/packages/pulkitjalan/geoip/config.php` to get the same effect.
87106

88-
### Get Data
107+
### Methods
89108

90109
Here are the avaliable methods to pull out the required information.
91110

src/Drivers/IPApiDriver.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
class IPApiDriver extends AbstractGeoIPDriver
99
{
10+
/**
11+
* @var string
12+
*/
13+
protected $key;
14+
1015
/**
1116
* @var string
1217
*/
@@ -24,7 +29,7 @@ public function __construct(array $config)
2429
{
2530
parent::__construct($config);
2631

27-
$this->requester = with(new Requester(new GuzzleClient()))->retry(2)->every(50);
32+
$this->requester = $this->create();
2833
}
2934

3035
/**
@@ -35,7 +40,7 @@ public function __construct(array $config)
3540
*/
3641
public function get($ip)
3742
{
38-
$data = $this->requester->url($this->baseUrl.$ip)->get()->json();
43+
$data = $this->requester->url($this->getUrl($ip))->get()->json();
3944

4045
if (array_get($data, 'status') === 'fail') {
4146
return [];
@@ -53,4 +58,24 @@ public function get($ip)
5358
'postalCode' => array_get($data, 'zip'),
5459
];
5560
}
61+
62+
protected function getUrl($ip)
63+
{
64+
return $this->baseUrl.$ip.(($this->key) ? '?key='.$this->key : '');
65+
}
66+
67+
/**
68+
* Create the ip-api driver based on config
69+
*
70+
* @return mixed
71+
*/
72+
protected function create()
73+
{
74+
if (array_get($this->config, 'key', false)) {
75+
$this->baseUrl = 'http://pro.ip-api.com/json/';
76+
$this->key = array_get($this->config, 'key');
77+
}
78+
79+
return with(new Requester(new GuzzleClient()))->retry(2)->every(50);
80+
}
5681
}

src/Exceptions/GeoIPException.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace PulkitJalan\GeoIP\Exceptions;
4+
5+
class GeoIPException extends \Exception
6+
{
7+
}

src/GeoIP.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace PulkitJalan\GeoIP;
44

5+
use PulkitJalan\GeoIP\Exceptions\GeoIPException;
6+
57
class GeoIP
68
{
79
/**
@@ -69,7 +71,7 @@ public function setIP($ip)
6971
*/
7072
public function getIP()
7173
{
72-
return ($this->ip) ?: array_get($_SERVER, 'HTTP_CLIENT_IP', array_get($_SERVER, 'HTTP_X_FORWARDED_FOR', array_get($_SERVER, 'HTTP_X_FORWARDED', array_get($_SERVER, 'HTTP_FORWARDED_FOR', array_get($_SERVER, 'HTTP_FORWARDED', array_get($_SERVER, 'REMOTE_ADDR'))))));
74+
return ($this->ip) ?: array_get($_SERVER, 'HTTP_CLIENT_IP', array_get($_SERVER, 'HTTP_X_FORWARDED_FOR', array_get($_SERVER, 'HTTP_X_FORWARDED', array_get($_SERVER, 'HTTP_FORWARDED_FOR', array_get($_SERVER, 'HTTP_FORWARDED', array_get($_SERVER, 'REMOTE_ADDR', '127.0.0.1'))))));
7375
}
7476

7577
/**
@@ -86,7 +88,12 @@ public function get($property = '')
8688
$data = array_get($this->store, $ip);
8789

8890
if (!$data) {
89-
$data = $this->getDriver()->get($ip);
91+
try {
92+
$data = $this->getDriver()->get($ip);
93+
} catch (\Exception $e) {
94+
throw new GeoIPException('Failed to get geoip data', 0, $e);
95+
}
96+
9097
$this->store[$ip] = $data;
9198
}
9299

src/Laravel/config/config.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
| Maxmind Database
2323
|--------------------------------------------------------------------------
2424
|
25-
| Required with type 'database'
2625
| Example: app_path().'/database/maxmind/GeoLite2-City.mmdb'
2726
|
2827
*/
@@ -32,11 +31,22 @@
3231
|--------------------------------------------------------------------------
3332
| Maxmind Web Service Info
3433
|--------------------------------------------------------------------------
35-
|
36-
| Required with type 'web'
37-
|
3834
*/
3935
'user_id' => '',
4036
'license_key' => '',
4137
],
38+
39+
/*
40+
|--------------------------------------------------------------------------
41+
| IP-API Driver
42+
|--------------------------------------------------------------------------
43+
*/
44+
'ip-api' => [
45+
/*
46+
|--------------------------------------------------------------------------
47+
| IP-API Pro Service Key
48+
|--------------------------------------------------------------------------
49+
*/
50+
'key' => '',
51+
],
4252
];

tests/GeoIPTest.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,6 @@ public function testMaxmindWebApiException()
7878
$this->setExpectedException('PulkitJalan\GeoIP\Exceptions\InvalidCredentialsException');
7979

8080
$geoip = new \PulkitJalan\GeoIP\GeoIP($config);
81-
$geoip = $geoip->setIP($this->validIP);
82-
83-
$geoip->get();
8481
}
8582

8683
public function testMaxmindWebApiAuthenticationException()
@@ -93,7 +90,7 @@ public function testMaxmindWebApiAuthenticationException()
9390
],
9491
];
9592

96-
$this->setExpectedException('GeoIp2\Exception\AuthenticationException');
93+
$this->setExpectedException('PulkitJalan\GeoIP\Exceptions\GeoIPException');
9794

9895
$geoip = new \PulkitJalan\GeoIP\GeoIP($config);
9996
$geoip = $geoip->setIP($this->validIP);
@@ -121,6 +118,23 @@ public function testMaxmindDatabase()
121118
$this->assertEquals($geoip->getCountry(), '');
122119
}
123120

121+
public function testIpApiProException()
122+
{
123+
$config = [
124+
'driver' => 'ip-api',
125+
'ip-api' => [
126+
'key' => 'test',
127+
],
128+
];
129+
130+
$this->setExpectedException('PulkitJalan\GeoIP\Exceptions\GeoIPException');
131+
132+
$geoip = new \PulkitJalan\GeoIP\GeoIP($config);
133+
$geoip = $geoip->setIP($this->validIP);
134+
135+
$this->assertEquals($geoip->getCountry(), '');
136+
}
137+
124138
public function testIpApi()
125139
{
126140
$config = [

0 commit comments

Comments
 (0)