基于 IP 地址的地理位置 - PHP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7766978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Geo Location based on IP Address - PHP
提问by Latox
We're looking for a fast and accurate way to get the visitors location based on their IP.
我们正在寻找一种快速准确的方法来根据访问者的 IP 获取他们的位置。
We have tried ipinfodb.com but their API made our website severely lag when making the API call.
我们已经尝试过 ipinfodb.com,但他们的 API 使我们的网站在进行 API 调用时严重滞后。
What other services do you suggest?
您还建议哪些其他服务?
回答by David Croft
Get Geo-IP Information
获取地理 IP 信息
Requests a geo-IP-server (netip.de) to check, returns where an IP is located (host, state, country, town).
请求地理 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: (.*?) #i';
$patterns["country"] = '#Country: (.*?) #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;
}
?>
回答by jacob justin
The best and latest is GeoplugginAPI its free and no limitations i am used for this my website http://spidersoft.infor location based download read more from this link
最好和最新的是 GeoplugginAPI,它是免费的,没有任何限制,我的网站http://spidersoft.in用于基于位置的下载,从这个链接阅读更多
回答by Ken Le
Try this one http://ip.codehelper.io/
Here is PHP code. The code will caching your request automatic, so your server will not send multiple request. Return IP Address, Country, City and more information.
这是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);
回答by RC.
The market leader in this space and one that provides an enterprise solution is Digital Element. They offer a wide array of APIs, including PHP, to access their server which you can install locally or access via a web service. Their data is of high quality and the performance of their solution is quite good. MaxMind is another option as well that receives good reviews.
该领域的市场领导者和提供企业解决方案的领导者是Digital Element。他们提供了包括 PHP 在内的各种 API 来访问他们的服务器,您可以在本地安装或通过 Web 服务访问这些服务器。他们的数据质量很高,他们的解决方案的性能非常好。MaxMind 也是另一个获得好评的选择。
For the best accuracy, you'll want to opt for a service or solution where you get weekly update(s) as this stuff can change quite a bit within a given network. Cost will depend on the frequency of updates, granularity of the geo data, and the number of additional fields or databases you want. Some providers offer language, demographics, company, and domain to name a few.
为了获得最佳准确性,您需要选择每周获得更新的服务或解决方案,因为这些内容在给定的网络中可能会发生很大变化。成本取决于更新频率、地理数据的粒度以及所需的附加字段或数据库的数量。一些提供商提供语言、人口统计、公司和域等。