PHP获得地理定位 - 逝去的青春

PHP获得地理定位

作者:jao 发布于:2012-12-16 12:49 分类:PHP教程

01 <?php
02        $ip=$_SERVER['REMOTE_ADDR'];
03        geoCheckIP($ip);
04   
05        // print_r(geoCheckIP($ip));
06        //Array ( [domain] => dslb-094-219-040-096.pools.arcor-ip.net [country] => DE - Germany [state] => Hessen [town] => Erzhausen )
07   
08        //Get an array with geoip-infodata
09        function geoCheckIP($ip)
10        {
11                //check, if the provided ip is valid
12                if(!filter_var($ip, FILTER_VALIDATE_IP))
13                {
14                        throw new InvalidArgumentException("IP is not valid");
15                }
16   
17                //contact ip-server
18                $response=@file_get_contents('http://www.netip.de/search?query='.$ip);
19                if (empty($response))
20                {
21                        throw new InvalidArgumentException("Error contacting Geo-IP-Server");
22                }
23   
24                //Array containing all regex-patterns necessary to extract ip-geoinfo from page
25                $patterns=array();
26                $patterns["domain"] = '#Domain: (.*?) #i';
27                $patterns["country"] = '#Country: (.*?) #i';
28                $patterns["state"] = '#State/Region: (.*?)<br#i';
29                $patterns["town"] = '#City: (.*?)<br#i';
30   
31                //Array where results will be stored
32                $ipInfo=array();
33   
34                //check response from ipserver for above patterns
35                foreach ($patterns as $key => $pattern)
36                {
37                        //store the result in array
38                        $ipInfo[$key] = preg_match($pattern,$response,$value) && !empty($value[1]) ?$value[1] : 'not found';
39                }
40   
41                return $ipInfo;
42        }
43   
44 ?>
45                    

标签: php教程 PHP 定位

et_highlighter

评论:

为你一人
2012-12-16 13:08
看不懂哦?能不能把空间任意位置签到改成这种http://down.mrpyx.cn/Method_mm.htm,用经纬度的,因为这样能显示正真的地图,把那个收听微博能改成自己的话
jao
2012-12-16 13:40
@为你一人:哦.这个我一个朋友做到了.我不会做
琪琪
2012-12-16 17:33
@jao:朋友在这里!
jao
2012-12-16 19:31
@琪琪:呵呵,你那个源码是不是要卖的?
Copyright © 2012-2013 逝去的青春 蜀ICP备13029051号-1 手机版 安全联盟 Theme by Admin73 sitemap