Finding Current Location (PHP)

Finding Current Location (PHP)

We can determine the location using the geolocation function. If there are relevant permissions issues or if the browser is not supported, we can use the API provided by Naver to obtain the location via IP address.

Before using it, please get your Naver API key at the following address.
(https://www.ncloud.com/product/applicationService/geoLocation)

<? 
	function makeSignature($secretKey, $method, $baseString, $timestamp, $accessKey)
	{
		$space = ' ';
		$newLine = "
";
		$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>Calculating Location...</title>
</head>
<body>
<div style="width: 100%; text-align: center; display: inline-block; ">
	<p>We are calculating the user's location for a smooth experience.</p>
	<p>Please <font color="RED"><strong>allow</strong></font> location access permission.</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>

Leave a Reply

이메일 주소는 공개되지 않습니다. (* 질문, 건의사항 등은 "질문게시판"을 이용해주세요)