Skip to content

Commit 1b87c99

Browse files
committed
Return A php array as result by default instead of json format.
1 parent 335ed16 commit 1b87c99

3 files changed

Lines changed: 20 additions & 14 deletions

File tree

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,21 @@ var_dump($country);
3737

3838
// For more precision
3939
$city = $this->geolocation->get_city();
40-
var_dump($city);
41-
42-
// Display error
43-
if(FALSE === $city)
40+
if($city === FALSE)
4441
var_dump($this->geolocation->get_error());
42+
else
43+
var_dump($city);
4544
```
4645

4746
# Additional parameters
4847

4948
You can change the result format within the config file,
49+
or leave it empty to return a PHP Array
5050

5151
Open `application/config/geolocation.php` :
5252

5353
```php
54-
$config['format'] = 'json'; // available format : xml|raw|json
54+
$config['format'] = 'json'; // available format : xml|raw|json or empty for php array
5555
```
5656

5757
# IpInfoDb API :

application/config/geolocation.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,16 @@
3131
| The API KEY : You can get yours from here http://ipinfodb.com/register.php
3232
|
3333
*/
34-
$config['api_key'] = 'YOUR_API_KEY';
34+
$config['api_key'] = 'YOUR_API_KEY_HERE';
3535

3636
/*
3737
|--------------------------------------------------------------------------
3838
| FORMAT
3939
|--------------------------------------------------------------------------
4040
|
41-
| The default format is JSON, but you can change it to XML or RAW format
41+
| The default format is a php array, but you can change it to XML, JSON or RAW format
42+
|
43+
| $config['format'] = ''; Returns a PHP array
4244
|
4345
| $config['format'] = 'json';
4446
|
@@ -47,4 +49,4 @@
4749
| $config['format'] = 'raw';
4850
|
4951
*/
50-
$config['format'] = 'json';
52+
$config['format'] = '';

application/libraries/Geolocation.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ class Geolocation
7474
private $ip_address = '';
7575

7676
/**
77-
* Returned format
77+
* Returned format, Leave it blank to return a PHP Array
7878
*
7979
* @var string
8080
*/
81-
private $format = 'json';
81+
private $format = '';
8282

8383
/**
8484
* Initialize the Geolocation library
@@ -123,7 +123,7 @@ public function initialize($params = array())
123123
*
124124
* @return Geolocation library
125125
*/
126-
public function set_key($api_key)
126+
public function set_api_key($api_key)
127127
{
128128
$this->api_key = $api_key;
129129

@@ -205,24 +205,28 @@ private function locate($type)
205205
return false;
206206
}
207207

208+
$as_array = empty($this->format);
209+
$this->format = $as_array ? 'json' : $this->format;
210+
208211
$url = $this->api
209212
. $this->api_version . '/'
210213
. $type . '/'
211214
. '?key=' . $this->api_key
212215
. '&ip=' . $this->ip_address
213216
. '&format=' . $this->format;
214217

215-
return $this->get_result($url);
218+
return $this->get_result($url, $as_array);
216219
}
217220

218221
/**
219222
* Locate the IP Address and return the data
220223
*
221224
* @param $url string
225+
* @param bool $as_array
222226
*
223227
* @return bool|string
224228
*/
225-
private function get_result($url){
229+
private function get_result($url, $as_array = FALSE){
226230
$data = @file_get_contents($url);
227231

228232
switch($this->format){
@@ -248,7 +252,7 @@ private function get_result($url){
248252
return FALSE;
249253
}
250254

251-
return $data;
255+
return $as_array ? (array) $result : $data;
252256
}
253257
}
254258

0 commit comments

Comments
 (0)