ThinkPHPÔõô»ñÈ¡Ðû²¼Õߵصã±ê×¢
Ò»¡¢Ê¹Óà IP µØµã»ñÈ¡Ðû²¼Õߵصã±ê×¢
ÔÚ»ñÈ¡Ðû²¼Õߵصã±êעʱ£¬¿ÉÒÔʹÓÃÆä IP µØµã¾ÙÐж¨Î»¡£ÔÚ ThinkPHP ÖУ¬¿ÉÒÔͨ¹ý getRequest() ÒªÁì»ñÈ¡Ä¿½ñÇëÇóµÄ IP µØµã£¬ÔÙʹÓõÚÈý·½µØÓßλÖÃЧÀÍ£¨Èç°Ù¶ÈµØͼ API£©¾ÙÐÐÆÊÎö£¬»ñÈ¡¶ÔÓ¦µÄµØÓßλÖÃÐÅÏ¢¡£
´úÂëʾÀý£º
// »ñÈ¡Ä¿½ñÇëÇóµÄ IP µØµã $client_ip = request()->ip(); // ʹÓðٶȵØͼ API »ñÈ¡µØÓßλÖÃÐÅÏ¢ $url = "http://api.map.m.linesum.net/location/ip?ak=ÄúµÄÃÜÔ¿&ip=$client_ip&coor=bd09ll"; $result = file_get_contents($url); $location = json_decode($result, true); $address = $location['content']['address'];
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬$client_ip ±äÁ¿´æ´¢Ä¿½ñÇëÇóµÄ IP µØµã£¬$url ±äÁ¿´æ´¢Å²ÓðٶȵØͼ API µÄ URL µØµã£¬$result ±äÁ¿´æ´¢ÆÊÎöЧ¹û£¬$location ±äÁ¿ÎªÆÊÎöЧ¹ûµÄÊý×éÐÎʽ£¬$address ±äÁ¿´æ´¢×îÖÕ»ñÈ¡µÄµØµã±ê×¢ÐÅÏ¢¡£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
¶þ¡¢Ê¹ÓÃä¯ÀÀÆ÷¶¨Î»»ñÈ¡Ðû²¼Õߵصã±ê×¢
³ýÁËʹÓà IP µØµã¶¨Î»»ñȡλÖñê¼Ç£¬»¹¿ÉÒÔʹÓÃä¯ÀÀÆ÷¶¨Î»À´»ñȡλÖñê¼Ç¡£ÔÚ ThinkPHP ÖУ¬¿ÉÒÔʹÓà JavaScript »ñÈ¡ä¯ÀÀÆ÷¶¨Î»£¬ÔÙʹÓõÚÈý·½µØÓßλÖÃЧÀÍ£¨ÈçÌÚѶµØͼ API£©¾ÙÐÐÆÊÎö£¬»ñÈ¡¶ÔÓ¦µÄµØµã±ê×¢ÐÅÏ¢¡£
´úÂëʾÀý£º
// Ò³ÃæÖÐÒýÈë JavaScript ¶¨Î»´úÂë navigator.geolocation.getCurrentPosition(function(position) { // »ñÈ¡ä¯ÀÀÆ÷¶¨Î»µÄ¾Î³¶È var lat = position.coords.latitude; var lng = position.coords.longitude; // ʹÓÃÌÚѶµØͼ API »ñÈ¡µØÓßλÖÃÐÅÏ¢ var url = "https://apis.map.qq.com/ws/geocoder/v1/?location=" + lat + "," + lng + "&key=ÄúµÄÃÜÔ¿&get_poi=0"; $.ajax({ url: url, dataType: "jsonp", success: function(result) { // ÆÊÎöЧ¹û£¬»ñÈ¡µØµã±ê×¢ÐÅÏ¢ var address = result.result.formatted_addresses.recommend; } }) });
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬navigator.geolocation.getCurrentPosition() ÒªÁì»ñÈ¡ä¯ÀÀÆ÷¶¨Î»£¬lat ºÍ lng ±äÁ¿´æ´¢»ñÈ¡µÄ¾Î³¶ÈÐÅÏ¢£¬url ±äÁ¿´æ´¢Å²ÓÃÌÚѶµØͼ API µÄ URL µØµã£¬success ²ÎÊýÄڵĴúÂë»ñÈ¡ÆÊÎöЧ¹û£¬²¢»ñÈ¡×îÖյĵصã±ê×¢ÐÅÏ¢¡£
ÒÔÉϾÍÊÇThinkPHPÔõô»ñÈ¡Ðû²¼Õߵصã±ê×¢µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡