Skip to content

Commit bbccb93

Browse files
committed
added telize driver
1 parent e987c76 commit bbccb93

File tree

9 files changed

+236
-93
lines changed

9 files changed

+236
-93
lines changed

README.md

Lines changed: 57 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,15 @@ GeoIP
1010
[![Latest Version](http://img.shields.io/packagist/v/pulkitjalan/geoip.svg?style=flat-square)](https://packagist.org/packages/pulkitjalan/geoip)
1111
[![Total Downloads](https://img.shields.io/packagist/dt/pulkitjalan/geoip.svg?style=flat-square)](https://packagist.org/packages/pulkitjalan/geoip)
1212

13-
This package requires PHP >= 5.4
13+
## Supported Drivers ([Services](#services))
14+
15+
* [IP-API](http://ip-api.com/)
16+
* [Telize](http://www.telize.com/)
17+
* [Maxmind](https://www.maxmind.com/)
18+
19+
## Requirements
20+
21+
* PHP >= 5.4
1422

1523
## Installation
1624

@@ -44,8 +52,6 @@ Next run `php artisan config:publish pulkitjalan/geoip` to publish the config fi
4452

4553
## Usage
4654

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

5157
Example:
@@ -61,7 +67,7 @@ $lat = $geoip->getLatitude(); // 51.5141
6167
$lon = $geoip->getLongitude(); // -3.1969
6268
```
6369

64-
### IP-API
70+
#### IP-API
6571

6672
To use the ip-api pro service you can set the options in your config.
6773

@@ -78,7 +84,22 @@ $config = [
7884
];
7985
```
8086

81-
### Maxmind
87+
#### Telize
88+
89+
To use the telize as the driver set the config, can also use https instead by setting `secure` to `true`.
90+
91+
Secure Example:
92+
```php
93+
$config = [
94+
'driver' => 'telize',
95+
'telize' => [
96+
// optionally set secure (https) connection (default: false)
97+
'secure' => true
98+
],
99+
];
100+
```
101+
102+
#### Maxmind
82103

83104
Maxmind support the database type and also web api type.
84105

@@ -107,19 +128,19 @@ $config = [
107128

108129
To use this package in Laravel, simply update the config file in `config/packages/pulkitjalan/geoip/config.php` to get the same effect.
109130

110-
### Methods
111-
112-
Here are the avaliable methods to pull out the required information.
131+
### Available Methods
113132

114-
Set IP (Optional)
133+
GeoIP will try to determin the ip using the following http headers: `HTTP_CLIENT_IP`, `HTTP_X_FORWARDED_FOR`, `HTTP_X_FORWARDED`, `HTTP_FORWARDED_FOR`, `HTTP_FORWARDED`, `REMOTE_ADDR` in this order. Optionally use the `setIp` method to set it.
115134

116135
```php
117-
$geoip->setIP('127.0.0.1');
136+
$geoip->setIp('127.0.0.1');
118137

119138
// Laravel
120-
GeoIP::setIP('127.0.0.1');
139+
GeoIP::setIp('127.0.0.1');
121140
```
122141

142+
There are a number of available methods to pull out the required information. All methods will return an empty string if data is unavailable.
143+
123144
Get latitude
124145

125146
```php
@@ -134,7 +155,7 @@ Get longitude
134155
```php
135156
$geoip->getLongitude();
136157

137-
//Laravel
158+
// Laravel
138159
GeoIP::getLongitude();
139160
```
140161

@@ -143,7 +164,7 @@ Get city
143164
```php
144165
$geoip->getCity();
145166

146-
//Laravel
167+
// Laravel
147168
GeoIP::getCity();
148169
```
149170

@@ -152,7 +173,7 @@ Get country
152173
```php
153174
$geoip->getCountry();
154175

155-
//Laravel
176+
// Laravel
156177
GeoIP::getCountry();
157178
```
158179

@@ -161,7 +182,7 @@ Get country code
161182
```php
162183
$geoip->getCountryCode();
163184

164-
//Laravel
185+
// Laravel
165186
GeoIP::getCountryCode();
166187
```
167188

@@ -170,7 +191,7 @@ Get region
170191
```php
171192
$geoip->getRegion();
172193

173-
//Laravel
194+
// Laravel
174195
GeoIP::getRegion();
175196
```
176197

@@ -179,7 +200,7 @@ Get region code
179200
```php
180201
$geoip->getRegionCode();
181202

182-
//Laravel
203+
// Laravel
183204
GeoIP::getRegionCode();
184205
```
185206

@@ -188,7 +209,7 @@ Get postal code
188209
```php
189210
$geoip->getPostalCode();
190211

191-
//Laravel
212+
// Laravel
192213
GeoIP::getPostalCode();
193214
```
194215

@@ -197,22 +218,31 @@ Get timezone
197218
```php
198219
$geoip->getTimezone();
199220

200-
//Laravel
221+
// Laravel
201222
GeoIP::getTimezone();
202223
```
203224

225+
Get isp (not supported on all drivers)
226+
227+
```php
228+
$geoip->getIsp();
229+
230+
// Laravel
231+
GeoIP::getIsp();
232+
```
233+
204234
Get all geo information
205235

206236
```php
207237
$geoip->get(); // returns array
208238

209-
//Laravel
239+
// Laravel
210240
GeoIP::get(); // returns array
211241
```
212242

213243
### Update Database
214244

215-
There is an update command avaliable to help with updating and installing a local geoip database. The following will download and install/update the database file to `/path/to/database.mmdb`.
245+
There is an update command available to help with updating and installing a local geoip database. The following will download and install/update the database file to `/path/to/database.mmdb`.
216246

217247
```php
218248
<?php
@@ -236,10 +266,14 @@ Once you have registered the service provider, you can use the command `php arta
236266

237267
## Services
238268

239-
### IP-API
269+
#### IP-API
240270

241271
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.
242272

243-
### Maxmind
273+
#### Telize
274+
275+
Telize is a free service that can be used as an alternative. It currently has no limitations.
276+
277+
#### Maxmind
244278

245279
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.

src/Drivers/AbstractGeoIPDriver.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,29 @@
22

33
namespace PulkitJalan\GeoIP\Drivers;
44

5+
use GuzzleHttp\Client as GuzzleClient;
6+
use PulkitJalan\Requester\Requester;
7+
58
abstract class AbstractGeoIPDriver
69
{
710
/**
811
* @var array
912
*/
1013
protected $config;
1114

15+
/**
16+
* @var \PulkitJalan\Requester\Requester
17+
*/
18+
protected $requester;
19+
1220
/**
1321
* @param array $config
1422
*/
1523
public function __construct(array $config)
1624
{
1725
$this->config = $config;
26+
27+
$this->requester = with(new Requester(new GuzzleClient()))->retry(2)->every(50);
1828
}
1929

2030
/**

src/Drivers/IPApiDriver.php

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,8 @@
22

33
namespace PulkitJalan\GeoIP\Drivers;
44

5-
use GuzzleHttp\Client as GuzzleClient;
6-
use PulkitJalan\Requester\Requester;
7-
85
class IPApiDriver extends AbstractGeoIPDriver
96
{
10-
/**
11-
* @var string
12-
*/
13-
protected $key;
14-
15-
/**
16-
* @var string
17-
*/
18-
protected $baseUrl = 'http://ip-api.com/json/';
19-
20-
/**
21-
* @var \PulkitJalan\Requester\Requester
22-
*/
23-
protected $requester;
24-
25-
/**
26-
* @param array $config
27-
*/
28-
public function __construct(array $config)
29-
{
30-
parent::__construct($config);
31-
32-
$this->requester = $this->create();
33-
}
34-
357
/**
368
* Get array of data using ip-api
379
*
@@ -56,31 +28,36 @@ public function get($ip)
5628
'regionCode' => array_get($data, 'regionName'),
5729
'timezone' => array_get($data, 'timezone'),
5830
'postalCode' => array_get($data, 'zip'),
31+
'organization' => array_get($data, 'org'),
32+
'isp' => array_get($data, 'isp'),
5933
];
6034
}
6135

62-
protected function getUrl($ip)
63-
{
64-
return $this->baseUrl.$ip.(($this->key) ? '?key='.$this->key : '');
65-
}
66-
6736
/**
68-
* Create the ip-api driver based on config
37+
* Get the ip-api url add key and
38+
* change base url if pro user
6939
*
70-
* @return mixed
40+
* @param string $ip
41+
* @return string
7142
*/
72-
protected function create()
43+
protected function getUrl($ip)
7344
{
7445
$protocol = 'http:';
7546
if (array_get($this->config, 'secure', false)) {
7647
$protocol = 'https:';
7748
}
7849

50+
// default to free service
51+
// free service does not support https
52+
$baseUrl = 'http://ip-api.com/json/';
53+
$key = '';
54+
55+
// if key is set change to pro service
7956
if (array_get($this->config, 'key', false)) {
80-
$this->baseUrl = $protocol.'://pro.ip-api.com/json/';
81-
$this->key = array_get($this->config, 'key');
57+
$baseUrl = $protocol.'//pro.ip-api.com/json/';
58+
$key = array_get($this->config, 'key');
8259
}
8360

84-
return with(new Requester(new GuzzleClient()))->retry(2)->every(50);
61+
return $baseUrl.$ip.(($key) ? '?key='.$key : '');
8562
}
8663
}

src/Drivers/MaxmindDriver.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,12 @@ public function get($ip)
6060
*/
6161
protected function create()
6262
{
63+
// if user_id and license_key are set then use the web service
6364
if (array_get($this->config, 'user_id', false)) {
6465
return $this->createWebClient();
6566
}
6667

68+
// if database file is set then use database service
6769
if (array_get($this->config, 'database', false)) {
6870
return $this->createDatabase();
6971
}
@@ -105,10 +107,11 @@ protected function createDatabase()
105107
throw new InvalidCredentialsException();
106108
}
107109

110+
// catch maxmind exception and throw geoip exception
108111
try {
109112
return new Reader($database);
110113
} catch (\MaxMind\Db\Reader\InvalidDatabaseException $e) {
111-
throw new InvalidDatabaseException($e->getMessage());
114+
throw new InvalidDatabaseException($e->getMessage(), $e->getCode(), $e);
112115
}
113116
}
114117
}

src/Drivers/TelizeDriver.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace PulkitJalan\GeoIP\Drivers;
4+
5+
use GuzzleHttp\Exception\RequestException;
6+
7+
class TelizeDriver extends AbstractGeoIPDriver
8+
{
9+
/**
10+
* Get array of data using telize
11+
*
12+
* @param string $ip
13+
* @return array
14+
*/
15+
public function get($ip)
16+
{
17+
try {
18+
$data = $this->requester->url($this->getUrl($ip))->get()->json();
19+
} catch (RequestException $e) {
20+
return [];
21+
}
22+
23+
return [
24+
'city' => array_get($data, 'city'),
25+
'country' => array_get($data, 'country'),
26+
'countryCode' => array_get($data, 'country_code'),
27+
'latitude' => array_get($data, 'latitude'),
28+
'longitude' => array_get($data, 'longitude'),
29+
'region' => array_get($data, 'region'),
30+
'regionCode' => array_get($data, 'region_code'),
31+
'timezone' => array_get($data, 'timezone'),
32+
'postalCode' => array_get($data, 'postal_code'),
33+
'isp' => array_get($data, 'isp'),
34+
];
35+
}
36+
37+
/**
38+
* Get the telize url
39+
*
40+
* @param string $ip
41+
* @return string
42+
*/
43+
protected function getUrl($ip)
44+
{
45+
$protocol = 'http:';
46+
if (array_get($this->config, 'secure', false)) {
47+
$protocol = 'https:';
48+
}
49+
50+
$baseUrl = $protocol.'//www.telize.com/geoip/';
51+
52+
return $baseUrl.$ip;
53+
}
54+
}

0 commit comments

Comments
 (0)