Rails:通过IP地址确定城市
2012-07-02 12:39
441 查看
一、对于Rails而言,主流方式应该是使用google库的插件geoip
github地址: https://github.com/simplificator/geoip
二、另一个geo_ip
使用之前需要在http://ipinfodb.com/ 注册一个用户,激活时可以获得自己的key。需要用自己的key来访问该网站的接口。
github地址: https://github.com/jeroenj/geo_ip
geokit(http://geokit.rubyforge.org/)是一个关于地理的工具,比如根据经纬度确定城市和距离之类,可以结合geo_ip做地理分析
三、过网络的IP查询API
这个办法IP库更新比较快。通用的库有几个比如google。
xml处理页面完全可以通过nokogiri等专门处理工具代替。
提供IP地址查询的API很多比如网易:
1.http://www.youdao.com/smartresult-xml/search.s?type=ip&q=IP地址
2. http://ip2loc.appspot.com/ #含有详细实例讲解, 但是由于GWF地址可能解析不到
3. 新浪的IP地址查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js
4. 新浪多地域测试方法:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=218.192.3.42
#这个比较全,但是有个别ip解析的不对,打个数据库没有更新吧。。。。
5. 搜狐IP地址查询接口(默认GBK):http://pv.sohu.com/cityjson
6. 搜狐IP地址查询接口(可设置编码):http://pv.sohu.com/cityjson?ie=utf-8
7. 搜狐另外的IP地址查询接口:http://txt.go.sohu.com/ip/soip
示例:
参考:/article/3770210.html
github地址: https://github.com/simplificator/geoip
require 'geoip' GeoIP.new('GeoLiteCity.dat').country('www.atlantis.sk') => ["www.atlantis.sk", "217.67.18.26", "SK", "SVK", "Slovakia", "EU", "02", "Bratislava", "", 48.15, 17.1167, nil, nil, "Europe/Bratislava"] Returned values are the requested hostname, the IP address as a dotted quad, Maxmind's country code, the ISO3166-1 country code, the ISO3166-2 country code, the ISO3166 country name, and the continent code. GeoIP.new('GeoCity.dat').city('github.com') => ["github.com", "207.97.227.239", "US", "USA", "United States", "NA", "CA", "San Francisco", "94110", 37.7484, -122.4156, 807, 415, "America/Los_Angeles"] Returned values are the country values followed by region or state name, city name, postal_code/zipcode, latitude, longitude, USA DMA code, USA area code, timezone name. Sorry it's not a Hash... historical. GeoIP.new('GeoIPASNum.dat').asn("www.fsb.ru") => ["AS8342", "RTComm.RU Autonomous System"]
二、另一个geo_ip
使用之前需要在http://ipinfodb.com/ 注册一个用户,激活时可以获得自己的key。需要用自己的key来访问该网站的接口。
github地址: https://github.com/jeroenj/geo_ip
# 209.85.227.104 = google.be (US) GeoIp.geolocation('209.85.227.104') #returns: { :status =>"OK", :ip =>"209.85.227.104" :country_code =>"US", :country_name =>"United States", :region_code =>"06", :region_name =>"California", :city =>"Mountain View", :zip_postal_code =>"94043", :latitude =>"37.4192", :longitude =>"-122.057" }
geokit(http://geokit.rubyforge.org/)是一个关于地理的工具,比如根据经纬度确定城市和距离之类,可以结合geo_ip做地理分析
#Find near latitude and longitude: Store.find(:all, :origin =>[37.792,-122.393], :within=>10) #Find near an address: Store.find(:all, :origin=>'100 Spear st, San Francisco, CA', :within=>10) #Order by distance from the center of a zipcode: Store.find(:all, :origin=>'94117', :within=>10, :order=>'distance asc') #Combine distance conditions with regular conditions Store.find(:all, :origin=>'94117', :within=>10, :conditions=>{:store_type=>'CAFE'})
三、过网络的IP查询API
这个办法IP库更新比较快。通用的库有几个比如google。
xml处理页面完全可以通过nokogiri等专门处理工具代替。
提供IP地址查询的API很多比如网易:
1.http://www.youdao.com/smartresult-xml/search.s?type=ip&q=IP地址
2. http://ip2loc.appspot.com/ #含有详细实例讲解, 但是由于GWF地址可能解析不到
3. 新浪的IP地址查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js
4. 新浪多地域测试方法:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=218.192.3.42
#这个比较全,但是有个别ip解析的不对,打个数据库没有更新吧。。。。
5. 搜狐IP地址查询接口(默认GBK):http://pv.sohu.com/cityjson
6. 搜狐IP地址查询接口(可设置编码):http://pv.sohu.com/cityjson?ie=utf-8
7. 搜狐另外的IP地址查询接口:http://txt.go.sohu.com/ip/soip
示例:
require "open-uri" require 'rubygems' require 'json' #如果有GET请求参数直接写在URI地址中 uri = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=218.192.3.42' response = nil open(uri) do |http| response = http.read end #response = Iconv.conv( "gb2312", "utf-8", response) @res = JSON::parse(response) p @res
参考:/article/3770210.html
相关文章推荐
- Rails:通过IP地址确定城市
- Rails 如何实现通过登录IP确定城市功能
- hive UDF 分享 通过IP地址获取 IDC/省份/城市
- 通过新浪IP服务器获得的当前客户端IP地址对应的国家、省份或直辖市、城市信息
- PHP通过访问第三方接口,根据IP地址获取所在城市
- js通过IP地址获取所在城市
- IP地址通过WebService得到城市
- IP地址通过WEBSERVICE得到城市
- 通过jquery 获取用户当前所在的城市名称和IP地址
- 通过IP地址获得用户所在城市的额天气信息的整理
- 通过IP地址获取IP所在城市和所使用运营商
- 通过ip地址实现城市定位
- 通过IP地址获取省份城市位置信息
- hive UDF 分享 通过IP地址获取IDC//省份/城市的信息http://my.oschina.net/wangjiankui/blog/263994
- 把IP地址通过WebService的方式转换城市
- Java 通过Request请求获取IP地址对应省份、城市
- JS 利用新浪接口通过IP地址获取当前所在城市
- 通过IP地址获取城市 QQWry.Dat
- php 通过 ip地址 进行城市定位
- 根据IP地址获取所在城市【通过新浪接口】