diff --git a/README.md b/README.md index 60e1756..c8affbb 100755 --- a/README.md +++ b/README.md @@ -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 : diff --git a/application/config/geolocation.php b/application/config/geolocation.php index 35064af..8dea734 100644 --- a/application/config/geolocation.php +++ b/application/config/geolocation.php @@ -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'; | @@ -47,4 +49,4 @@ | $config['format'] = 'raw'; | */ -$config['format'] = 'json'; +$config['format'] = ''; diff --git a/application/libraries/Geolocation.php b/application/libraries/Geolocation.php index 3461210..e56b875 100644 --- a/application/libraries/Geolocation.php +++ b/application/libraries/Geolocation.php @@ -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 @@ -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; @@ -205,6 +205,9 @@ 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 . '/' @@ -212,17 +215,18 @@ private function locate($type) . '&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){ @@ -248,7 +252,7 @@ private function get_result($url){ return FALSE; } - return $data; + return $as_array ? (array) $result : $data; } }