如何从IP地址确定一个邮政编码和城市?

我正在寻找开放的API或3D派对产品的build议,这将有助于将IP地址转换为邮政编码和城市,以制作特定于地理位置的网站内容。

我使用的是ip2nation: http ://www.ip2nation.com

这是一个完全免费的国家映射IP的MySQL数据库。 他们在他们的网站上包含一些PHP脚本示例。

我在我的WordPress博客上使用它和Ozh的免费IP到Nation WordPress插件

即使你没有使用WordPress,也要下载插件,因为它会给你一套很好的PHP代码来访问数据库。 他甚至有一个很好的小国家国旗,你可以旁边显示,或者而不是国家名称,这是我自己显示标志旁边我的博客的评论者的名字。

真的,你不需要这个API。 所有你需要的是数据库和能够编写PHP和SQL代码来访问它。

我不会推荐ip2location的随意使用。 他们为他们的数据库收取很多费用,而且他们的数据库非常大,比大多数人需要更多的细节。

但是ip2nation只能进入国家级别。 如果你需要城市和邮政编码(尽pipe事实上他们可能是错误的,如其他答案/评论所述),那么除了ip2location, 你可以尝试GeoBytes 。 它们具有特别适用于特定于地理位置的网站内容的组件,如GeoSelect: http : //www.geobytes.com/GeoSelect.htm

在Google上search“ip to zipcode” 。

最好的(意味着易于集成和免费)的方式,我发现可以在这里 。 该服务是完全免费的,不需要任何本地安装,不会强制您在您的网站上放置横幅(而不是IP2Location)。 他们唯一的限制是查询限制,这是非常合理的:你可以每2秒触发一次查询,即使你做更频繁的查询,结果也会慢一些。 请享用!

我希望所有想要从IP地址获取位置的用户都可以通过使用这个小代码来获得正确的解决scheme。

 $ip_addr = $_SERVER['REMOTE_ADDR']; $geoplugin = unserialize( file_get_contents('http://www.geoplugin.net/php.gp?ip='.$ip_addr) ); if ( is_numeric($geoplugin['geoplugin_latitude']) && is_numeric($geoplugin['geoplugin_longitude']) ) { $lat = $geoplugin['geoplugin_latitude']; $long = $geoplugin['geoplugin_longitude']; } 

Geocoder.ca提供了大量的IP地址信息(仅限于北美地区),例如http://geocoder.ca/99.229.2.190?geoit=xml (也支持geoit = json和jsonp)

 <result> <geodata> <latt>43.623262</latt> <longt>-79.749543</longt> <city>MISSISSAUGA</city> <prov>ON</prov> <postal>L5N7Z9</postal> <stnumber>1837</stnumber> <staddress>Samuelson CIR</staddress> <TimeZone>America/Toronto</TimeZone> <AreaCode>289</AreaCode> <confidence>1</confidence> <intersection> <street1>Samuelson Cir</street1> <street2>Stockbridge Crt</street2> <lattx>43.622696</lattx> <longtx>-79.748706</longtx> <city>MISSISSAUGA</city> <prov>ON</prov> <distance>0.179</distance> </intersection> </geodata> </result> 

以下是我发现使用http://ipinfodb.com/ip_locator.php获取其信息的代码片段的修改版本。; 请记住,您也可以使用API​​密钥申请API密钥,并直接使用该API来获取您认为合适的信息。 正如你可以看到http://ipinfodb.com/ip_location_api.php ,他们有从PHP到JavaScript到ASP.Net的所有例子。 如上所述,下面不需要一个键,因为它拉动他们的公共页面和正则expression式的页面来获得指定的信息。 钥匙是免费的。

片段

 function detect_location($ip=NULL, $asArray=FALSE) { if (empty($ip)) { if (!empty($_SERVER['HTTP_CLIENT_IP'])) { $ip = $_SERVER['HTTP_CLIENT_IP']; } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; } else { $ip = $_SERVER['REMOTE_ADDR']; } } elseif (!is_string($ip) || strlen($ip) < 1 || $ip == '127.0.0.1' || $ip == 'localhost') { $ip = '8.8.8.8'; } $url = 'http://ipinfodb.com/ip_locator.php?ip=' . urlencode($ip); $i = 0; $content; $curl_info; while (empty($content) && $i < 5) { $ch = curl_init(); $curl_opt = array( CURLOPT_FOLLOWLOCATION => 1, CURLOPT_HEADER => 0, CURLOPT_RETURNTRANSFER => 1, CURLOPT_URL => $url, CURLOPT_TIMEOUT => 1, CURLOPT_REFERER => 'http://' . $_SERVER['HTTP_HOST'], ); if (isset($_SERVER['HTTP_USER_AGENT'])) $curl_opt[CURLOPT_USERAGENT] = $_SERVER['HTTP_USER_AGENT']; curl_setopt_array($ch, $curl_opt); $content = curl_exec($ch); if (!is_null($curl_info)) $curl_info = curl_getinfo($ch); curl_close($ch); } $araResp = array(); if (preg_match('{<li>City : ([^<]*)</li>}i', $content, $regs)) $araResp['city'] = trim($regs[1]); if (preg_match('{<li>State/Province : ([^<]*)</li>}i', $content, $regs)) $araResp['state'] = trim($regs[1]); if (preg_match('{<li>Country : ([^<]*)}i', $content, $regs)) $araResp['country'] = trim($regs[1]); if (preg_match('{<li>Zip or postal code : ([^<]*)</li>}i', $content, $regs)) $araResp['zip'] = trim($regs[1]); if (preg_match('{<li>Latitude : ([^<]*)</li>}i', $content, $regs)) $araResp['latitude'] = trim($regs[1]); if (preg_match('{<li>Longitude : ([^<]*)</li>}i', $content, $regs)) $araResp['longitude'] = trim($regs[1]); if (preg_match('{<li>Timezone : ([^<]*)</li>}i', $content, $regs)) $araResp['timezone'] = trim($regs[1]); if (preg_match('{<li>Hostname : ([^<]*)</li>}i', $content, $regs)) $araResp['hostname'] = trim($regs[1]); $strResp = ($araResp['city'] != '' && $araResp['state'] != '') ? ($araResp['city'] . ', ' . $araResp['state']) : 'UNKNOWN'; return $asArray ? $araResp : $strResp; } 

使用

 detect_location(); // returns "CITY, STATE" based on user IP detect_location('xxx.xxx.xxx.xxx'); // returns "CITY, STATE" based on IP you provide detect_location(NULL, TRUE); // based on user IP // returns array(8) { ["city"] => "CITY", ["state"] => "STATE", ["country"] => "US", ["zip"] => "xxxxx", ["latitude"] => "xx.xxxxxx", ["longitude"] => "-xx.xxxxxx", ["timezone"] => "-07:00", ["hostname"] => "xx-xx-xx-xx.host.name.net" } detect_location('xxx.xxx.xxx.xxx', TRUE); // based on IP you provide // returns array(8) { ["city"] => "CITY", ["state"] => "STATE", ["country"] => "US", ["zip"] => "xxxxx", ["latitude"] => "xx.xxxxxx", ["longitude"] => "-xx.xxxxxx", ["timezone"] => "-07:00", ["hostname"] => "xx-xx-xx-xx.host.name.net" } 

在Ruby中…这些是步骤

  1. 安装gem geocoder gem install geocoder
  2. 在一个ruby脚本中,有这些线。

    require "geocoder"

    puts Geocoder.address('76.95.251.102'); //相应地更改IP地址。

  3. 运行脚本获取城市,州,邮编,国家

output: Bellflower, CA 90706, United States