Home  >  Article  >  Backend Development  >  How to implement positioning function in php

How to implement positioning function in php

藏色散人
藏色散人Original
2022-11-07 09:10:281553browse

How to implement the positioning function in php: 1. Register as a Baidu user and become a map open platform developer; 2. Obtain the service key; 3. Obtain the longitude and latitude through the Baidu Map API interface; 4. Position through the API interface Just go to the specific location.

How to implement positioning function in php

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.

How to implement positioning function in php?

php implements IP positioning

Register as a Baidu user, become a map open platform developer, and obtain a service key. Obtain the latitude and longitude through the Baidu Map API interface, and then perform global reverse geocoding services through the API interface. Simply put, it is to locate the specific location again through the longitude and latitude.

The code is:

<!DOCTYPE  html>
<html>
<head>
<title>IP定位具体位置</title>
</head>
<body>
<form  action="" method="POST">
请输入公网IP:<input   type="text"   name="ip">
<input  type="submit"  name="submit" >
</form>
</body>
</html>
<?php
header("Content-Type:text/html;charset=utf-8;");
$ip=@$_POST[&#39;ip&#39;];
//正则匹配IP
if (preg_match(&#39;/^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$/&#39;,$ip)){
$url_ip="http://api.map.baidu.com/location/ip?ip=$ip&ak=您的ak&coor=bd09ll";
//注册后得到的服务密钥ak
$str=file_get_contents($url_ip);
$arr=json_decode($str,true);
if ($arr["status"]==0){
$x=$arr[&#39;content&#39;][&#39;point&#39;][&#39;x&#39;];
$y=$arr[&#39;content&#39;][&#39;point&#39;][&#39;y&#39;];
$url="http://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location=$y,$x&output=json&pois=1&ak=您的ak";
//注意y代表纬度,x代表精度
$str=file_get_contents($url);
//var_dump($str);
//以下自由发挥
preg_match("/{.+}/", $str, $result);
$arr=json_decode($result[0],true);
$num=count($arr["result"]["pois"]);
for ($i=0; $i < $num; $i++) {
echo &#39;<font   color="#ff0000">&#39;.$ip."-----".$arr["result"]["pois"][$i]["addr"]."<br/>";
}
}else{
echo "<h2>not find IP</h2>";
}
}else{
echo "<h2>not  really  IP</h2>";
}
?>

Please replace ak

Attach a picture:

How to implement positioning function in php

IP positioning

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to implement positioning function in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn