@@ -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