Finding the current location (PHP)

Finding the current location (PHP)

The location is obtained through the Geolocation function, and if it is a browser that is not related or supported, the location is obtained with the IPI through the API provided by Naver.

Before use, you get Naver's API key to the address below.
(https://www.ncloud.com/product/applicationService/geoLocation)

<? 
	function makeSignature($secretKey, $method, $baseString, $timestamp, $accessKey)
	{
		$space = ' ';
		$newLine = "\n";
		$hmac = $method.$space.$baseString.$newLine.$timestamp.$newLine.$accessKey;
		$signautue = base64_encode(hash_hmac('sha256', $hmac, $secretKey,true));
		return $signautue;
	}

	if($_POST['order']== 'Geolocation')
 {
 $ HostNameurl = 'https://geolocation.apigw.ntruss.com';
 $ Requesturl = '/Geolocation/V2/Geolocation';
 $ Accesskey = 'Please enter AccessKey.';
 $ Secretkey = 'Please enter Secretkey.';

 $ IP = $ _server ['Remote_addr'];
		$timestamp = round(microtime(true) * 1000);

		$baseString = $requestUrl.'?ip='.$ip.'&ext=t&responseFormatType=json';

		$signautue = makeSignature($secretKey, 'GET', $baseString, $timestamp, $accessKey);
		$url = $hostNameUrl.$baseString;

		$is_post = false;
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_POST, $is_post);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		$headers = array();
		$headers[] = 'X-NCP-APIGW-TIMESTAMP: '.$timestamp;
		$headers[] = 'X-NCP-IAM-ACCESS-KEY: '.$accessKey;
		$headers[] = 'X-NCP-APIGW-SIGNATURE-V2: '.$signautue;

		curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
		$response = json_decode(curl_exec ($ch), true);

		if($response['geoLocation'])
		{
			$lat = $response['geoLocation']['lat'];
			$lng = $response['geoLocation']['long'];
 } else {
 $ lat = 37.535053;
 $ LNG = 127.147263;
 }

 Echo json_encode (Array ('Latitude' => $ LAT, 'Longitude' => $ LNG));
 exit;
 }

 ?>
 <HTML Lang = "KO">
 <HEAD>
 <META CHARSET = "UTF-8">
 <TITLE> Location is being calculated ... </Title>
 </head>
 <body>
 <DIV style = "width: 100%; text-align: center; display: inline-block;">
 <P> We are calculating the user's location for smooth use. </P>
 <P> Please allow the location of the location <font color = "red"> <strong> </strong> </font>. </P>
 </div>

 <Script>
 Function setpositionBygeo (POS)
 {
 document.cookie = "latitude =" + pos.coords.latitude;
 document.cookie = "longitude =" + pos.coords.longitude;

 location.href = "<? = $ _ get ['URL'] ? $_GET['url'] : '/'?>";
	}

	function setPositionByIP()
	{
		var xhr = new XMLHttpRequest();
		var dat = new FormData();

		dat.append("order", "geolocation");

		xhr.open("POST", window.location.pathname);
		xhr.send(dat);

		xhr.onload = function()
		{
			if(xhr.status === 200 || xhr.status === 201)
			{
				var res = JSON.parse(xhr.responseText);

				if(res.latitude&&res.longitude)
				{
					document.cookie = "latitude=" + res.latitude;
					document.cookie = "longitude=" + res.longitude;


					location.href = "<?=$_GET['url'] ? $_GET['url'] : '/'?>";
				}
			}
		};
	}

	window.onload = function() {
		if (navigator.geolocation)
			navigator.geolocation.getCurrentPosition(setPositionByGeo, setPositionByIP);
		else
			setPositionByIP();
	}
</script>
</body>
</html>
guest
0 Comments
Inline Feedbacks
View all comments