Skip to content

Commit 4f3a2cb

Browse files
committed
updated with new structure and updated readme
1 parent 666efec commit 4f3a2cb

File tree

13 files changed

+635
-22
lines changed

13 files changed

+635
-22
lines changed

README.md

Lines changed: 148 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
GeoIP
22
=============
33

4-
> GeoIP Wrapper
4+
> Geoip Wrapper with Laravel Support
55
66
[![Build Status](http://img.shields.io/travis/pulkitjalan/geoip.svg?style=flat-square)](https://travis-ci.org/pulkitjalan/geoip)
77
[![Scrutinizer Code Quality](http://img.shields.io/scrutinizer/g/pulkitjalan/geoip/master.svg?style=flat-square)](https://scrutinizer-ci.com/g/pulkitjalan/geoip/)
@@ -11,3 +11,150 @@ GeoIP
1111
[![Total Downloads](https://img.shields.io/packagist/dt/pulkitjalan/geoip.svg?style=flat-square)](https://packagist.org/packages/pulkitjalan/geoip)
1212

1313
This package requires PHP >=5.4
14+
15+
## Installation
16+
17+
Install via composer - edit your `composer.json` to require the package.
18+
19+
```js
20+
"require": {
21+
"pulkitjalan/geoip": "dev-master"
22+
}
23+
```
24+
25+
Then run `composer update` in your terminal to pull it in.
26+
27+
### Laravel
28+
29+
There is a Laravel service provider and facade available.
30+
31+
Add the following to the `providers` array in your `config/app.php`
32+
33+
```php
34+
'PulkitJalan\GeoIP\Laravel\GeoIPServiceProvider'
35+
```
36+
37+
Next add the following to the `aliases` array in your `config/app.php`
38+
39+
```php
40+
'GeoIP' => 'PulkitJalan\GeoIP\Laravel\Facades\GeoIP'
41+
```
42+
43+
Next run `php artisan config:publish pulkitjalan/geoip` to publish the config file.
44+
45+
## Usage
46+
47+
The geoip class takes a config array as the first parameter. Example:
48+
49+
```php
50+
<?php
51+
52+
use PulkitJalan\GeoIP\GeoIP
53+
54+
$config = ['driver' => 'ip-api'];
55+
56+
$geoip = new GeoIP($config);
57+
```
58+
59+
Supported Drivers: [maxmind](https://www.maxmind.com/) and [ip-api](http://ip-api.com/)
60+
61+
Maxmind support the database type and also web api type.
62+
63+
Database Example:
64+
```php
65+
$config = [
66+
'driver' => 'maxmind',
67+
'maxmind' => [
68+
'database' => '/path/to/database.mmdb',
69+
],
70+
];
71+
```
72+
73+
Web API Example:
74+
```php
75+
$config = [
76+
'driver' => 'maxmind',
77+
'maxmind' => [
78+
'user_id' => 'YOUR MAXMIND USER ID',
79+
'license_key' => 'YOUR MAXMIND LICENSE KEY'
80+
],
81+
];
82+
```
83+
84+
Here are the avaliable methods to pull out the required information.
85+
86+
Set IP (Optional)
87+
88+
```php
89+
$geoip->setIP('127.0.0.1');
90+
```
91+
92+
Get latitude
93+
94+
```php
95+
$geoip->getLatitude();
96+
```
97+
98+
Get longitude
99+
100+
```php
101+
$geoip->getLongitude();
102+
```
103+
104+
Get city
105+
106+
```php
107+
$geoip->getCity();
108+
```
109+
110+
Get country
111+
112+
```php
113+
$geoip->getCountry();
114+
```
115+
116+
Get country code
117+
118+
```php
119+
$geoip->getCountryCode();
120+
```
121+
122+
Get region
123+
124+
```php
125+
$geoip->getRegion();
126+
```
127+
128+
Get region code
129+
130+
```php
131+
$geoip->getRegionCode();
132+
```
133+
134+
Get postal code
135+
136+
```php
137+
$geoip->getPostalCode();
138+
```
139+
140+
Get timezone
141+
142+
```php
143+
$geoip->getTimezone();
144+
```
145+
146+
Get all geo information
147+
148+
```php
149+
$geoip->get();
150+
```
151+
152+
## Services
153+
154+
### Maxmind
155+
156+
You can use the free database from maxmind or their web api service. You can download the free database service [here](http://dev.maxmind.com/geoip/geoip2/geolite2/) or enter your `user id` and `license key` in the config.
157+
158+
### IP-API
159+
160+
IP-API is a free service that can also be used instead of the database file or the paid maxmind service. They do have some limitations please have a look at the [docs](http://ip-api.com/docs/) first.

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "pulkitjalan/geoip",
3-
"description": "Geoip wrapper",
3+
"description": "Geoip Wrapper with Laravel Support",
44
"homepage": "https://github.com/pulkitjalan/geoip",
5-
"keywords": ["geoip"],
5+
"keywords": ["geoip", "maxmind", "ip-api"],
66
"license": "MIT",
77
"authors": [
88
{
@@ -12,6 +12,7 @@
1212
"require": {
1313
"php": ">=5.4.0",
1414
"illuminate/support": "~4|~5",
15+
"pulkitjalan/requester": "1.*",
1516
"geoip2/geoip2": "2.*"
1617
},
1718
"require-dev": {

phpunit.xml.dist

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
<whitelist>
1919
<directory suffix=".php">src/</directory>
2020
<exclude>
21-
<file>src/GeoIPServiceProvider.php</file>
22-
<directory suffix=".php">src/config/</directory>
23-
<directory suffix=".php">src/Facades/</directory>
21+
<directory suffix=".php">src/Laravel/</directory>
2422
</exclude>
2523
</whitelist>
2624
</filter>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace PulkitJalan\GeoIP\Drivers;
4+
5+
abstract class AbstractGeoIPDriver
6+
{
7+
/**
8+
* @var array
9+
*/
10+
protected $config;
11+
12+
/**
13+
* @param array $config
14+
*/
15+
public function __construct(array $config)
16+
{
17+
$this->config = $config;
18+
}
19+
20+
/**
21+
* Get GeoIP info from IP
22+
*
23+
* @param string $ip
24+
* @return array
25+
*/
26+
abstract public function get($ip);
27+
}

src/Drivers/IPApiDriver.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace PulkitJalan\GeoIP\Drivers;
4+
5+
use GuzzleHttp\Client as GuzzleClient;
6+
use PulkitJalan\Requester\Requester;
7+
8+
class IPApiDriver extends AbstractGeoIPDriver
9+
{
10+
/**
11+
* @var string
12+
*/
13+
protected $baseUrl = 'http://ip-api.com/json/';
14+
15+
/**
16+
* @var \PulkitJalan\Requester\Requester
17+
*/
18+
protected $requester;
19+
20+
public function __construct(array $config)
21+
{
22+
parent::__construct($config);
23+
24+
$this->requester = with(new Requester(new GuzzleClient()))->retry(2)->every(50);
25+
}
26+
27+
/**
28+
* Get array of data using ip-api
29+
*
30+
* @param string $ip
31+
* @return array
32+
*/
33+
public function get($ip)
34+
{
35+
$data = $this->requester->url($this->baseUrl.$ip)->get()->json();
36+
37+
if (array_get($data, 'status') === 'fail') {
38+
return [];
39+
}
40+
41+
return [
42+
'city' => array_get($data, 'city'),
43+
'country' => array_get($data, 'country'),
44+
'countryCode' => array_get($data, 'countryCode'),
45+
'latitude' => array_get($data, 'lat'),
46+
'longitude' => array_get($data, 'lon'),
47+
'region' => array_get($data, 'region'),
48+
'regionCode' => array_get($data, 'regionName'),
49+
'timezone' => array_get($data, 'timezone'),
50+
'postalCode' => array_get($data, 'zip'),
51+
];
52+
}
53+
}

src/Drivers/MaxmindDriver.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
namespace PulkitJalan\GeoIP\Drivers;
4+
5+
use PulkitJalan\GeoIP\Exceptions\InvalidCredentialsException;
6+
use GeoIp2\Exception\AddressNotFoundException;
7+
use GeoIp2\WebService\Client;
8+
use GeoIp2\Database\Reader;
9+
10+
class MaxmindDriver extends AbstractGeoIPDriver
11+
{
12+
/**
13+
* @var \GeoIp2\WebService\Client|\GeoIp2\Database\Reader
14+
*/
15+
protected $maxmind;
16+
17+
/**
18+
* @param array $config
19+
*/
20+
public function __construct(array $config)
21+
{
22+
parent::__construct($config);
23+
24+
$this->maxmind = $this->create();
25+
}
26+
27+
/**
28+
* Get array of data using Maxmind
29+
*
30+
* @param string $ip
31+
* @return array
32+
*/
33+
public function get($ip)
34+
{
35+
try {
36+
$data = $this->maxmind->city($ip);
37+
} catch (AddressNotFoundException $e) {
38+
return [];
39+
}
40+
41+
return [
42+
'city' => $data->city->name,
43+
'country' => $data->country->name,
44+
'countryCode' => $data->country->isoCode,
45+
'latitude' => $data->location->latitude,
46+
'longitude' => $data->location->longitude,
47+
'region' => $data->mostSpecificSubdivision->name,
48+
'regionCode' => $data->mostSpecificSubdivision->isoCode,
49+
'timezone' => $data->location->timeZone,
50+
'postalCode' => $data->postal->code,
51+
];
52+
}
53+
54+
/**
55+
* Create the maxmind driver based on config
56+
*
57+
* @return mixed
58+
* @throws \PulkitJalan\GeoIP\Exceptions\InvalidCredentialsException
59+
*/
60+
protected function create()
61+
{
62+
if (array_get($this->config, 'user_id', false)) {
63+
return $this->createWebClient();
64+
}
65+
66+
if (array_get($this->config, 'database', false)) {
67+
return $this->createDatabase();
68+
}
69+
70+
throw new InvalidCredentialsException();
71+
}
72+
73+
/**
74+
* Create the maxmind web client
75+
*
76+
* @return \GeoIp2\WebService\Client
77+
* @throws \PulkitJalan\GeoIP\Exceptions\InvalidCredentialsException
78+
*/
79+
protected function createWebClient()
80+
{
81+
$userId = array_get($this->config, 'user_id', false);
82+
$licenseKey = array_get($this->config, 'license_key', false);
83+
84+
// check and make sure they are set
85+
if (!$userId || !$licenseKey) {
86+
throw new InvalidCredentialsException();
87+
}
88+
89+
return new Client($userId, $licenseKey);
90+
}
91+
92+
/**
93+
* Create the maxmind database reader
94+
*
95+
* @return \GeoIp2\Database\Reader
96+
* @throws \PulkitJalan\GeoIP\Exceptions\InvalidCredentialsException
97+
*/
98+
protected function createDatabase()
99+
{
100+
$database = array_get($this->config, 'database', false);
101+
102+
// check if file exists first
103+
if (!$database || !file_exists($database)) {
104+
throw new InvalidCredentialsException();
105+
}
106+
107+
return new Reader($database);
108+
}
109+
}
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 InvalidCredentialsException extends \Exception
6+
{
7+
}
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 InvalidDriverException extends \Exception
6+
{
7+
}

0 commit comments

Comments
 (0)