<?php 

/**
* Return longitude and latitude Coordinates from the given IP
*/
function IP_location($ip){
	// $ch = curl_init('https://ipinfo.io/'.$ip.'/json');
        // curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        // $json = curl_exec($ch);
        // curl_close($ch);
        // $addrDetailsArr = json_decode($json, true);
		// $location='-';
			// if(isset($addrDetailsArr['country']) && !empty($addrDetailsArr['country'])){
				// $location=$addrDetailsArr['country'].'-'.$addrDetailsArr['city'];
			// }
			
		// $ch = curl_init('http://ipwhois.app/json/' . $ip);
        // curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        // $json = curl_exec($ch);
        // curl_close($ch);
   
        // $addrDetailsArr = json_decode($json, true);
		// print_r($addrDetailsArr); die;
		// $location='-';
		// if(isset($addrDetailsArr['country']) && !empty($addrDetailsArr['country'])){
			// $location=$addrDetailsArr['country'].'-'.$addrDetailsArr['city'];
		// }
		$ch = curl_init('https://get.geojs.io/v1/ip/geo/' . $ip . '.json');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $json = curl_exec($ch);
        curl_close($ch);
   
        $addrDetailsArr = json_decode($json, true);
		$location='-';
		if(isset($addrDetailsArr['country']) && !empty($addrDetailsArr['country'])){
			$location=$addrDetailsArr['country'];
		}
		return $location;
	
}
function IPtoCoords($ip){
	//$geopluginURL='http://www.geoplugin.net/php.gp?ip='.$ip;
	//$addrDetailsArr = @unserialize(file_get_contents('http://ip-api.com/php/'.$ip));
		$ch = curl_init('http://ipwhois.app/json/' . $ip);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $json = curl_exec($ch);
        curl_close($ch);
   
        $addrDetailsArr = json_decode($json, true);
	//$addrDetailsArr = unserialize(file_get_contents($geopluginURL));
		$CountryName	= '';
		$RegionName 	='';
		$City 			= '';	
		$Latitude 		= '';
		$Longitude 		= '';
	/* if($addrDetailsArr && $addrDetailsArr['status'] == 'success'){ */

	if($addrDetailsArr && $addrDetailsArr['success'] == '1'){
		$CountryName	= $addrDetailsArr['country'];
		$RegionName 	= $addrDetailsArr['region'];
		$City 			= $addrDetailsArr['city'];	
/* 		$Latitude 		= $addrDetailsArr['lat'];
		$Longitude 		= $addrDetailsArr['lon']; */
		$Latitude 		= $addrDetailsArr['latitude'];
		$Longitude 		= $addrDetailsArr['longitude'];
	}

	$location = "";
	
	if ($Latitude == "" || $Longitude == "")
	{
		return false;	
	}
	else
	{
		if ($City != "")
			$location = $City;
		else if ($RegionName != "")
			$location = $RegionName;
		else if ($CountryName != "")
			$location = $CountryName;
	}
	return array('location' => $location.' - '.$CountryName, 'longitude' => $Longitude, 'latitude' => $Latitude);
}
function IPtoCoords_geoplugin($ip){
	$geopluginURL='http://www.geoplugin.net/php.gp?ip='.$ip;
	$addrDetailsArr = unserialize(file_get_contents($geopluginURL));
	$CountryName	= $addrDetailsArr['geoplugin_countryName'];
	$RegionName 	= $addrDetailsArr['geoplugin_regionName'];
	$City 			= $addrDetailsArr['geoplugin_city'];	
	$Latitude 		= $addrDetailsArr['geoplugin_latitude'];
	$Longitude 		= $addrDetailsArr['geoplugin_longitude'];
	$location = "";
	
	if ($Latitude == "" || $Longitude == "")
	{
		return false;	
	}
	else
	{
		if ($City != "")
			$location = $City;
		else if ($RegionName != "")
			$location = $RegionName;
		else if ($CountryName != "")
			$location = $CountryName;
	}
	return array('location' => $location, 'longitude' => $Longitude, 'latitude' => $Latitude);
}

function IPtoCoords_bk($ip)
{

	$dom = new DOMDocument();
	$ipcheck = ip2long($ip);
	
	if($ipcheck == -1 || $ipcheck === false)
    	@trigger_error('Invalid IP, what are you doing? :|', E_USER_ERROR);
	else
		$uri = 'http://freegeoip.appspot.com/xml/' . $ip;

	@$dom->load($uri);
	
	$CountryName	= $dom->getElementsByTagName('CountryName')->item(0)->nodeValue;
	$RegionName 	= $dom->getElementsByTagName('RegionName')->item(0)->nodeValue;
	$City 			= $dom->getElementsByTagName('City')->item(0)->nodeValue;	
	$Latitude 		= $dom->getElementsByTagName('Latitude')->item(0)->nodeValue;
	$Longitude 		= $dom->getElementsByTagName('Longitude')->item(0)->nodeValue;
	
	$location = "";
	
	if ($Latitude == "" || $Longitude == "")
	{
		return false;	
	}
	else
	{
		if ($City != "")
			$location = $City;
		else if ($RegionName != "")
			$location = RegionName;
		else if ($CountryName != "")
			$location = CountryName;
	}
	return array('location' => $location, 'longitude' => $Longitude, 'latitude' => $Latitude);
}

/*
function IPtoCoords($ip)
{
	@$dom = new DOMDocument();

	@$ipcheck = ip2long($ip);
    if($ipcheck == -1 || $ipcheck === false)
    	@trigger_error('Invalid IP, what are you doing? :|', E_USER_ERROR);
	else
		$uri = 'http://api.hostip.info/?ip=' . $ip;

	@$dom->load($uri);
	@$location = (strpos($dom->getElementsByTagName('name')->item(1)->nodeValue, 'Unknown') === false)
					? $dom->getElementsByTagName('name')->item(1)->nodeValue
					: $dom->getElementsByTagName('countryAbbrev')->item(0)->nodeValue;
	
	if($location == 'XX')
		return false;
	else
	{
		@$dom->load('http://local.yahooapis.com/MapsService/V1/geocode?appid='.Yahoo_Key.'&location='.$location);
		$longitude = @$dom->getElementsByTagName('Longitude')->item(0)->nodeValue;
		$latitude  = @$dom->getElementsByTagName('Latitude')->item(0)->nodeValue;
		return array('location' => $location, 'longitude' => $longitude, 'latitude' => $latitude);
	}
}
*/
?>