Skip to content

Commit

Permalink
Return A php array as result by default instead of json format.
Browse files Browse the repository at this point in the history
  • Loading branch information
JCSama committed Apr 12, 2015
1 parent 335ed16 commit 1b87c99
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,21 @@ var_dump($country);

// For more precision
$city = $this->geolocation->get_city();
var_dump($city);

// Display error
if(FALSE === $city)
if($city === FALSE)
var_dump($this->geolocation->get_error());
else
var_dump($city);
```

# Additional parameters

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

Open `application/config/geolocation.php` :

```php
$config['format'] = 'json'; // available format : xml|raw|json
$config['format'] = 'json'; // available format : xml|raw|json or empty for php array
```

# IpInfoDb API :
Expand Down
8 changes: 5 additions & 3 deletions application/config/geolocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@
| The API KEY : You can get yours from here http://ipinfodb.com/register.php
|
*/
$config['api_key'] = 'YOUR_API_KEY';
$config['api_key'] = 'YOUR_API_KEY_HERE';

/*
|--------------------------------------------------------------------------
| FORMAT
|--------------------------------------------------------------------------
|
| The default format is JSON, but you can change it to XML or RAW format
| The default format is a php array, but you can change it to XML, JSON or RAW format
|
| $config['format'] = ''; Returns a PHP array
|
| $config['format'] = 'json';
|
Expand All @@ -47,4 +49,4 @@
| $config['format'] = 'raw';
|
*/
$config['format'] = 'json';
$config['format'] = '';
16 changes: 10 additions & 6 deletions application/libraries/Geolocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ class Geolocation
private $ip_address = '';

/**
* Returned format
* Returned format, Leave it blank to return a PHP Array
*
* @var string
*/
private $format = 'json';
private $format = '';

/**
* Initialize the Geolocation library
Expand Down Expand Up @@ -123,7 +123,7 @@ public function initialize($params = array())
*
* @return Geolocation library
*/
public function set_key($api_key)
public function set_api_key($api_key)
{
$this->api_key = $api_key;

Expand Down Expand Up @@ -205,24 +205,28 @@ private function locate($type)
return false;
}

$as_array = empty($this->format);
$this->format = $as_array ? 'json' : $this->format;

$url = $this->api
. $this->api_version . '/'
. $type . '/'
. '?key=' . $this->api_key
. '&ip=' . $this->ip_address
. '&format=' . $this->format;

return $this->get_result($url);
return $this->get_result($url, $as_array);
}

/**
* Locate the IP Address and return the data
*
* @param $url string
* @param bool $as_array
*
* @return bool|string
*/
private function get_result($url){
private function get_result($url, $as_array = FALSE){
$data = @file_get_contents($url);

switch($this->format){
Expand All @@ -248,7 +252,7 @@ private function get_result($url){
return FALSE;
}

return $data;
return $as_array ? (array) $result : $data;
}
}

Expand Down

0 comments on commit 1b87c99

Please sign in to comment.