現在の場所を見つける(PHP)

現在の場所を見つける(PHP)

場所はジオロケーション関数を介して取得され、関連またはサポートされていないブラウザの場合、Naverが提供するAPIを介してIPIで取得されます。

使用する前に、以下のアドレスにNaverのAPIキーを取得します。
(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 = 'AccessKeyを入力してください。';
 $ SecretKey = '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'];
 } それ以外 {
 $ lat = 37.535053;
 $ lng = 127.147263;
 }

 echo json_encode(array( 'latitude' => $ lat、 '経度' => $ lng));
 出口;
 }

 ?>
 <html lang = "ko">
 <head>
 <メタcharset = "utf-8">
 <title>場所が計算されています... </title>
 </head>
 <body>
 <div style = "width:100%; text-align:center; display:inline-block;">
 <p>スムーズな使用のためにユーザーの場所を計算しています。</p>
 <p>場所の場所を許可してください<font color = "red"> <strong> </strong> </font>。</p>
 </div>

 <スクリプト>
 関数SetPositionByGeo(POS)
 {
 document.cookie = "latitude =" + pos.coords.latitude;
 document.cookie = "resuded =" + 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