Home > Article > Backend Development > How to implement positioning function in php
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.
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['ip']; //正则匹配IP if (preg_match('/^(?: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}$/',$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['content']['point']['x']; $y=$arr['content']['point']['y']; $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 '<font color="#ff0000">'.$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:
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!