<?php
//include 'settings.php';
//ob_clean();
//ob_implicit_flush();
function GetGeolocationInnotech($geox,$geoy){
	
	global $server;
	global $port;
	
	$smes = '{"P":{"X":"'.$geox.'","Y":"'.$geoy.'"},"T":"1"}';

	//$ObjReportRun= new Report($table);
	//$ObjReportRun->ObjLogs->WriteLogs("GetGeolocationInnotech: ".$smes);
	
	$reply='';
    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die();
    socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1) or die();
	$connection = socket_connect($socket, $server, $port) or die();
	if (socket_write($socket, $smes) === false) fclose($socket);
	$reply = socket_read($socket,1024);
	
	if($reply){
		$jn = json_decode($reply, true);
		if($jn['P']['E']){
			return $reply="";
		}else{ 
			return $jn['P']['A'];
		}
	}else{ return $reply=""; }
	
	socket_shutdown($socket,2);
	//return $reply;
	socket_close($socket);
}

function GetGeolocationInnotech2($geox,$geoy){
			//$url = 'http://geocode-maps.yandex.ru/1.x/?format=json&geocode='.$LatLng;
			/*$url = 'http://localhost:81/WebGeoCoding/?={"P":{"X":"'.$geox.'","Y":"'.$geoy.'"},"T":"1"}';
			$CH = curl_init();
            curl_setopt($CH, CURLOPT_URL, $url);
            curl_setopt($CH, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($CH, CURLOPT_HEADER,0); //Change this to a 1 to return headers
            curl_setopt($CH, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
            curl_setopt($CH, CURLOPT_FOLLOWLOCATION, 1);
            $response = json_decode(curl_exec($CH), true);
			return curl_exec($CH);*/
			global $GeoPort;
			if( $curl = curl_init() ) {
			curl_setopt($curl, CURLOPT_URL, 'http://localhost:'.$GeoPort.'/WebGeoCoding/');
			curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
			curl_setopt($curl, CURLOPT_POST, true);
			curl_setopt($curl, CURLOPT_POSTFIELDS, '{"P":{"X":"'.$geox.'","Y":"'.$geoy.'"},"T":"1"}');
			$out = curl_exec($curl);
			curl_close($curl);
			
			if($out){
				$jn = json_decode($out, true);
			if($jn['P']['E']){
				return $out="";
			}else{ 
				return $jn['P']['A'];
			}
			}else{ return $out=""; }
			
			}else{ return $out=""; }

			
}

//echo GetGeolocationInnotech2($_REQUEST['geox'].'+'.$_REQUEST['geoy']);
//echo GetGeolocationInnotech2($_REQUEST['geox'],$_REQUEST['geoy']);

//ob_end_flush();
//ob_clean();
?>
