基于IP地址的地理位置 - PHP

时间:2011-10-14 11:31:51

标签: php geolocation geoip

我们正在寻找一种快速准确的方式来根据他们的IP获取访问者位置。

我们已经尝试了ipinfodb.com,但他们的API使我们的网站在进行API调用时严重滞后。

您建议使用其他哪些服务?

4 个答案:

答案 0 :(得分:38)

获取地理IP信息

请求地理IP服务器(netip.de)检查,返回IP所在的位置(主机,州,国家,城镇)。

<?php
       $ip='94.219.40.96';
       print_r(geoCheckIP($ip));
       //Array ( [domain] => dslb-094-219-040-096.pools.arcor-ip.net [country] => DE - Germany [state] => Hessen [town] => Erzhausen )

       //Get an array with geoip-infodata
       function geoCheckIP($ip)
       {
               //check, if the provided ip is valid
               if(!filter_var($ip, FILTER_VALIDATE_IP))
               {
                       throw new InvalidArgumentException("IP is not valid");
               }

               //contact ip-server
               $response=@file_get_contents('http://www.netip.de/search?query='.$ip);
               if (empty($response))
               {
                       throw new InvalidArgumentException("Error contacting Geo-IP-Server");
               }

               //Array containing all regex-patterns necessary to extract ip-geoinfo from page
               $patterns=array();
               $patterns["domain"] = '#Domain: (.*?)&nbsp;#i';
               $patterns["country"] = '#Country: (.*?)&nbsp;#i';
               $patterns["state"] = '#State/Region: (.*?)<br#i';
               $patterns["town"] = '#City: (.*?)<br#i';

               //Array where results will be stored
               $ipInfo=array();

               //check response from ipserver for above patterns
               foreach ($patterns as $key => $pattern)
               {
                       //store the result in array
                       $ipInfo[$key] = preg_match($pattern,$response,$value) && !empty($value[1]) ? $value[1] : 'not found';
               }

               return $ipInfo;
       }

?>

答案 1 :(得分:4)

最好的和最新的是GeoplugginAPI它是免费的,没有限制我用于我的网站http://spidersoft.in基于位置的下载阅读更多来自此链接

http://geoplugin.com

答案 2 :(得分:3)

试试这个http://ip.codehelper.io/

这是PHP代码。代码将自动缓存您的请求,因此您的服务器不会发送多个请求。返回IP地址,国家,城市和更多信息。

<?php
// Required Libraries
require_once("ip.codehelper.io.php");
require_once("php_fast_cache.php");

// New Class
$_ip = new ip_codehelper();

// Detect Real IP Address & Location
$real_client_ip_address = $_ip->getRealIP();
$visitor_location       = $_ip->getLocation($real_client_ip_address);

// Output result
echo $visitor_location['Country']."";
echo "<pre>";
print_r($visitor_location);

答案 3 :(得分:2)

这个领域的市场领导者和提供企业解决方案的领导者是Digital Element。他们提供各种API,包括PHP,以访问他们可以在本地安装或通过Web服务访问的服务器。他们的数据质量很高,他们的解决方案的性能非常好。 MaxMind也是另一种选择,可以获得良好的评价。

为了获得最佳准确性,您需要选择每周更新的服务或解决方案,因为这些内容可能会在给定网络中发生相当大的变化。成本取决于更新的频率,地理数据的粒度以及所需的其他字段或数据库的数量。一些提供商提供语言,人口统计,公司和域名等等。

相关问题