Skip to content

Commit 42dceb9

Browse files
committed
added freegeoip
1 parent 482aeb1 commit 42dceb9

File tree

8 files changed

+211
-47
lines changed

8 files changed

+211
-47
lines changed

README.md

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ GeoIP
1212

1313
## Supported Drivers ([Services](#services))
1414

15+
* [FreeGeoIP](https://freegeoip.net/)
1516
* [IP-API](http://ip-api.com/)
16-
* [Telize](https://market.mashape.com/fcambus/telize/)
1717
* [Maxmind](https://www.maxmind.com/)
18+
* [Telize](https://market.mashape.com/fcambus/telize/)
1819

1920
## Requirements
2021

@@ -71,33 +72,44 @@ $lat = $geoip->getLatitude(); // 51.5141
7172
$lon = $geoip->getLongitude(); // -3.1969
7273
```
7374

74-
#### IP-API
75+
#### FreeGeoIP
7576

76-
To use the ip-api pro service you can set the options in your config.
77+
To use the freegeoip as the driver set the config.
7778

78-
Pro Example:
79+
Example:
7980
```php
8081
$config = [
81-
'driver' => 'ip-api',
82-
'ip-api' => [
83-
'key' => 'YOUR IP-API KEY',
82+
'driver' => 'freegeoip',
83+
'freegeoip' => [
84+
'secure' => true,
85+
],
86+
];
87+
```
8488

85-
// optionally set secure (https) connection (default: false)
86-
'secure' => true
89+
Custom install example:
90+
```php
91+
$config = [
92+
'driver' => 'freegeoip',
93+
'freegeoip' => [
94+
'url' => 'freegeoip.example.com', // or with a port (freegeoip.example.com:8080)
95+
'secure' => true, // or false
8796
],
8897
];
8998
```
9099

91-
#### Telize
100+
#### IP-API
92101

93-
To use the telize as the driver set the config, and your api key.
102+
To use the ip-api pro service you can set the options in your config.
94103

95-
Example:
104+
Pro Example:
96105
```php
97106
$config = [
98-
'driver' => 'telize',
99-
'telize' => [
107+
'driver' => 'ip-api',
108+
'ip-api' => [
100109
'key' => 'YOUR IP-API KEY',
110+
111+
// optionally set secure (https) connection (default: false)
112+
'secure' => true
101113
],
102114
];
103115
```
@@ -127,6 +139,20 @@ $config = [
127139
];
128140
```
129141

142+
#### Telize
143+
144+
To use the telize as the driver set the config, and your api key.
145+
146+
Example:
147+
```php
148+
$config = [
149+
'driver' => 'telize',
150+
'telize' => [
151+
'key' => 'YOUR IP-API KEY',
152+
],
153+
];
154+
```
155+
130156
### Laravel
131157

132158
To use this package in Laravel, simply update the config file in `config/geoip.php` to get the same effect. The driver can be set using the `GEOIP_DRIVER` env.
@@ -278,14 +304,18 @@ Once you have registered the service provider, you can use the command `php arti
278304

279305
## Services
280306

281-
#### IP-API
307+
#### FreeGeoIP
282308

283-
IP-API is a free (or paid) service that can also be used instead of the database file or the paid maxmind service. They do have some limitations on the free service so please have a look at the [docs](http://ip-api.com/docs/) first.
309+
Freegeoip is a free service that can also be used instead of the database file or the paid maxmind service. They do have some limitations so please have a look at the [website](https://freegeoip.net/) first. You can also run a [custom install](https://github.com/fiorix/freegeoip) and use that instead.
284310

285-
#### Telize
311+
#### IP-API
286312

287-
Telize offers a JSON IP and GeoIP REST API allowing to get a visitor IP address and to query location information from any IP address. It outputs JSON-encoded IP geolocation data, and supports both Cross-origin resource sharing (CORS) and JSONP.
313+
IP-API is a free (or paid) service that can also be used instead of the database file or the paid maxmind service. They do have some limitations on the free service so please have a look at the [docs](http://ip-api.com/docs/) first.
288314

289315
#### Maxmind
290316

291317
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.
318+
319+
#### Telize
320+
321+
Telize offers a JSON IP and GeoIP REST API allowing to get a visitor IP address and to query location information from any IP address. It outputs JSON-encoded IP geolocation data, and supports both Cross-origin resource sharing (CORS) and JSONP.

src/Drivers/FreeGeoIPDriver.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace PulkitJalan\GeoIP\Drivers;
4+
5+
class FreeGeoIPDriver extends AbstractGeoIPDriver
6+
{
7+
/**
8+
* Get array of data using freegeoip.
9+
*
10+
* @param string $ip
11+
*
12+
* @return array
13+
*/
14+
public function get($ip)
15+
{
16+
$data = $this->getRaw($ip);
17+
18+
if (empty($data) || array_get($data, 'latitude') === 0 && array_get($data, 'longitude') === 0) {
19+
return $this->getDefault();
20+
}
21+
22+
return [
23+
'city' => array_get($data, 'city'),
24+
'country' => array_get($data, 'country_name'),
25+
'countryCode' => array_get($data, 'country_code'),
26+
'latitude' => (float) number_format(array_get($data, 'latitude'), 5),
27+
'longitude' => (float) number_format(array_get($data, 'longitude'), 5),
28+
'region' => array_get($data, 'region_name'),
29+
'regionCode' => array_get($data, 'region_code'),
30+
'timezone' => array_get($data, 'time_zone'),
31+
'postalCode' => array_get($data, 'zip_code'),
32+
];
33+
}
34+
35+
/**
36+
* Get the raw GeoIP info using freegeoip.
37+
*
38+
* @param string $ip
39+
*
40+
* @return array
41+
*/
42+
public function getRaw($ip)
43+
{
44+
return json_decode($this->guzzle->get($this->getUrl($ip))->getBody(), true);
45+
}
46+
47+
/**
48+
* Get the freegeoip url
49+
*
50+
* @param string $ip
51+
*
52+
* @return string
53+
*/
54+
protected function getUrl($ip)
55+
{
56+
$protocol = 'http://';
57+
if (array_get($this->config, 'secure', true)) {
58+
$protocol = 'https://';
59+
}
60+
61+
return $protocol.array_get($this->config, 'url', 'freegeoip.net').'/json/'.$ip;
62+
}
63+
}

src/Drivers/IPApiDriver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ public function get($ip)
2323
'city' => array_get($data, 'city'),
2424
'country' => array_get($data, 'country'),
2525
'countryCode' => array_get($data, 'countryCode'),
26-
'latitude' => array_get($data, 'lat'),
27-
'longitude' => array_get($data, 'lon'),
26+
'latitude' => (float) number_format(array_get($data, 'lat'), 5),
27+
'longitude' => (float) number_format(array_get($data, 'lon'), 5),
2828
'region' => array_get($data, 'regionName'),
2929
'regionCode' => array_get($data, 'region'),
3030
'timezone' => array_get($data, 'timezone'),

src/Drivers/MaxmindDriver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ public function get($ip)
4444
'city' => $data->city->name,
4545
'country' => $data->country->name,
4646
'countryCode' => $data->country->isoCode,
47-
'latitude' => $data->location->latitude,
48-
'longitude' => $data->location->longitude,
47+
'latitude' => (float) number_format($data->location->latitude, 5),
48+
'longitude' => (float) number_format($data->location->longitude, 5),
4949
'region' => $data->mostSpecificSubdivision->name,
5050
'regionCode' => $data->mostSpecificSubdivision->isoCode,
5151
'timezone' => $data->location->timeZone,

src/Drivers/TelizeDriver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ public function get($ip)
3838
'city' => array_get($data, 'city'),
3939
'country' => array_get($data, 'country'),
4040
'countryCode' => array_get($data, 'country_code'),
41-
'latitude' => array_get($data, 'latitude'),
42-
'longitude' => array_get($data, 'longitude'),
41+
'latitude' => (float) number_format(array_get($data, 'latitude'), 5),
42+
'longitude' => (float) number_format(array_get($data, 'longitude'), 5),
4343
'region' => array_get($data, 'region'),
4444
'regionCode' => array_get($data, 'region_code'),
4545
'timezone' => array_get($data, 'timezone'),

src/GeoIPManager.php

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

33
namespace PulkitJalan\GeoIP;
44

5+
use PulkitJalan\GeoIP\Drivers\FreeGeoIPDriver;
56
use PulkitJalan\GeoIP\Drivers\IPApiDriver;
67
use PulkitJalan\GeoIP\Drivers\MaxmindDriver;
78
use PulkitJalan\GeoIP\Drivers\TelizeDriver;
@@ -41,23 +42,23 @@ public function getDriver($driver = null)
4142
}
4243

4344
/**
44-
* Get the ip-api driver.
45+
* Get the freegeoip driver.
4546
*
46-
* @return \PulkitJalan\GeoIP\IPApiDriver
47+
* @return \PulkitJalan\GeoIP\FreeGeoIPDriver
4748
*/
48-
protected function createIpApiDriver(array $data)
49+
protected function createFreegeoipDriver(array $data)
4950
{
50-
return new IPApiDriver($data);
51+
return new FreeGeoIPDriver($data);
5152
}
5253

5354
/**
54-
* Get the telize driver.
55+
* Get the ip-api driver.
5556
*
56-
* @return \PulkitJalan\GeoIP\TelizeDriver
57+
* @return \PulkitJalan\GeoIP\IPApiDriver
5758
*/
58-
protected function createTelizeDriver(array $data)
59+
protected function createIpApiDriver(array $data)
5960
{
60-
return new TelizeDriver($data);
61+
return new IPApiDriver($data);
6162
}
6263

6364
/**
@@ -69,4 +70,14 @@ protected function createMaxmindDriver(array $data)
6970
{
7071
return new MaxmindDriver($data);
7172
}
73+
74+
/**
75+
* Get the telize driver.
76+
*
77+
* @return \PulkitJalan\GeoIP\TelizeDriver
78+
*/
79+
protected function createTelizeDriver(array $data)
80+
{
81+
return new TelizeDriver($data);
82+
}
7283
}

src/config/config.php

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
| GeoIP Driver Type
77
|--------------------------------------------------------------------------
88
|
9-
| Supported: "ip-api", "telize", "maxmind"
9+
| Supported: "freegeoip", "ip-api", "maxmind", "telize"
1010
|
1111
*/
1212
'driver' => env('GEOIP_DRIVER', 'ip-api'),
@@ -20,48 +20,57 @@
2020

2121
/*
2222
|--------------------------------------------------------------------------
23-
| IP-API Driver
23+
| Free GeoIP Driver
2424
|--------------------------------------------------------------------------
2525
*/
26-
'ip-api' => [
26+
'freegeoip' => [
2727
/*
2828
|--------------------------------------------------------------------------
29-
| IP-API Pro Service Key
29+
| Free GeoIP url
3030
|--------------------------------------------------------------------------
3131
|
32-
| Check out pro here: https://signup.ip-api.com/
32+
| Url to self hosted freegeoip (including port) without protocol
3333
|
3434
*/
35-
'key' => env('GEOIP_IPAPI_KEY'),
35+
'url' => env('GEOIP_FREEGEOIP_URL'),
3636

3737
/*
3838
|--------------------------------------------------------------------------
39-
| IP-API Secure connection
39+
| Free GeoIP Secure connection
4040
|--------------------------------------------------------------------------
4141
|
4242
| Use http or https
43-
| Only applicable with the Pro service
4443
|
4544
*/
46-
'secure' => env('GEOIP_IPAPI_SECURE', true),
45+
'secure' => env('GEOIP_FREEGEOIP_SECURE', true),
4746
],
4847

4948
/*
5049
|--------------------------------------------------------------------------
51-
| Telize Driver
50+
| IP-API Driver
5251
|--------------------------------------------------------------------------
5352
*/
54-
'telize' => [
53+
'ip-api' => [
5554
/*
5655
|--------------------------------------------------------------------------
57-
| Telize Service Key
56+
| IP-API Pro Service Key
5857
|--------------------------------------------------------------------------
5958
|
60-
| Get your API key here: https://market.mashape.com/fcambus/telize
59+
| Check out pro here: https://signup.ip-api.com/
6160
|
6261
*/
62+
'key' => env('GEOIP_IPAPI_KEY'),
6363

64-
'key' => env('GEOIP_TELIZE_KEY'),
64+
/*
65+
|--------------------------------------------------------------------------
66+
| IP-API Secure connection
67+
|--------------------------------------------------------------------------
68+
|
69+
| Use http or https
70+
| Only applicable with the Pro service
71+
|
72+
*/
73+
'secure' => env('GEOIP_IPAPI_SECURE', true),
6574
],
6675

6776
/*
@@ -88,4 +97,22 @@
8897
'user_id' => env('GEOIP_MAXMIND_USER_ID'),
8998
'license_key' => env('GEOIP_MAXMIND_LICENSE_KEY'),
9099
],
100+
101+
/*
102+
|--------------------------------------------------------------------------
103+
| Telize Driver
104+
|--------------------------------------------------------------------------
105+
*/
106+
'telize' => [
107+
/*
108+
|--------------------------------------------------------------------------
109+
| Telize Service Key
110+
|--------------------------------------------------------------------------
111+
|
112+
| Get your API key here: https://market.mashape.com/fcambus/telize
113+
|
114+
*/
115+
116+
'key' => env('GEOIP_TELIZE_KEY'),
117+
],
91118
];

0 commit comments

Comments
 (0)