jcadima
2/23/2016 - 9:17 PM

Location From IP address, JSON Objects

Location From IP address, JSON Objects

http://stackoverflow.com/questions/409999/getting-the-location-from-an-ip-address
2nd source
http://stackoverflow.com/questions/263392/handling-data-in-a-php-json-object

if file get contents is to slow
http://stackoverflow.com/questions/3629504/php-file-get-contents-very-slow-when-using-full-url
<?php

//EX1:
	$location = file_get_contents('http://freegeoip.net/json/'.$_SERVER['REMOTE_ADDR']);
	$location_output = json_decode($location);
	// Use:
	echo $location_output->country_code;  // outputs country code, see below
	
 print_r($location);
 {

"ip":"73.179.220.111",
"country_code":"US",
"country_name":"United States",
"region_code":"FL",
"region_name":"Florida",
"city":"Miami",
"zip_code":"33155",
"time_zone":"America/New_York",
"latitude":25.736,
"longitude":-80.3111,
"metro_code":528

}
 

// if file get contents is too slow:
$context = stream_context_create(array('http' => array('header'=>'Connection: close\r\n')));
file_get_contents("http://www.something.com/somepage.html",false,$context);
 
 
 // EX2
 $jsonurl = "http://search.twitter.com/trends.json";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json);

foreach ( $json_output->trends as $trend )
{
    echo "{$trend->name}\n";
}