In this article I will explain how to get…
October 23, 2017
获取访问者的IP, 定位访问中IP,Geoip DB
How to use?
Geoip-db.com allows you to find out the geographic location of your website’s visitor by IP address. Easily identify the location, country, city, hostname, network owner and other characteristics of your Internet users by using one of our services. We provide two kind of services, either a JSON-object or JSONP callback function is returned. You can use them with one of the following URLs. The returned objects have always the same structure:
JSON object
https://geoip-db.com/json
{ "country_code":"US", "country_name":"United States", "city":"San Francisco", "postal":94103, "latitude":37.7758, "longitude":-122.4128, "IPv4":"108.208.53.181", "state":"California" }
JSONP callback
- https://geoip-db.com/jsonp
- https://geoip-db.com/jsonp/{ipv4-address}
- https://geoip-db.com/jsonp/{ipv6-address}
Examples:
https://geoip-db.com/jsonp
https://geoip-db.com/jsonp/82.196.6.157
https://geoip-db.com/jsonp/0:0:0:0:0:FFFF:52C4:069D
callback({ "country_code":"US", "country_name":"United States", "city":"San Francisco", "postal":94103, "latitude":37.7758, "longitude":-122.4128, "IPv4":"108.208.53.181", "state":"California" })
A jQuery example with html markup
<!DOCTYPE html> <html> <head> <title>GEOIP DB - jQuery example</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> </head> <body> <div>Country: <span id="country"></span> <div>State: <span id="state"></span> <div>City: <span id="city"></span> <div>Latitude: <span id="latitude"></span> <div>Longitude: <span id="longitude"></span> <div>IP: <span id="IPv4"></span> <script> $.ajax({ url: "https://geoip-db.com/jsonp", jsonpCallback: "callback", dataType: "jsonp", success: function( location ) { $('#country').html(location.country_name); $('#state').html(location.state); $('#city').html(location.city); $('#latitude').html(location.latitude); $('#longitude').html(location.longitude); $('#ip').html(location.IPv4); } }); </script> </body> </html>
A plain JSONP javascript example, no jQuery required.
<!DOCTYPE html> <html> <head> <title>GEOIP DB - javascript example</title> </head> <body> <div>Country: <span id="country"></span> <div>State: <span id="state"></span> <div>City: <span id="city"></span> <div>Latitude: <span id="latitude"></span> <div>Longitude: <span id="longitude"></span> <div>IP: <span id="IPv4"></span> <script> var country = document.getElementById('country'); var state = document.getElementById('state'); var city = document.getElementById('city'); var postal = document.getElementById('postal'); var latitude = document.getElementById('latitude'); var longitude = document.getElementById('longitude'); var ip = document.getElementById('ipv4'); function callback(data) { country.innerHTML = data.country_name; state.innerHTML = data.state; city.innerHTML = data.city; postal.innerHTML = data.postal; latitude.innerHTML = data.latitude; longitude.innerHTML = data.longitude; ip.innerHTML = data.IPv4; } var script = document.createElement('script'); script.type = 'text/javascript'; script.src = 'https://geoip-db.com/jsonp'; var h = document.getElementsByTagName('script')[0]; h.parentNode.insertBefore(script, h); </script> </body> </html>
PHP Example
<?php $json = file_get_contents('https://geoip-db.com/json'); $data = json_decode($json); print $data->country_code . '<br>'; print $data->country_name . '<br>'; print $data->state . '<br>'; print $data->city . '<br>'; print $data->postal . '<br>'; print $data->latitude . '<br>'; print $data->longitude . '<br>'; print $data->IPv4 . '<br>'; ?>
A C# .NET WPF example
// Add System.Web.Extensions as a reference to your project // And add the namespace System.Web.Script.Serialization to your code using System.Web.Script.Serialization InitializeComponent(); using (var webClient = new System.Net.WebClient()){ var data = webClient.DownloadString("https://geoip-db.com/json"); JavaScriptSerializer jss = new JavaScriptSerializer(); var d = jss.Deserialize<dynamic>(data); string country_code = d["country_code"]; string country_name = d["country_name"]; string city = d["city"]; string postal = d["postal"]; string state = d["state"]; string ipv4 = d["IPv4"]; decimal latitude = d["latitude"]; decimal longitude = d["longitude"]; // Make sure you have the corresponding labels set up in your GUI this.LblCountryCode.Content = country_code; this.LblCountryName.Content = country_name; this.LblCity.Content = city; this.LblPostal.Content = postal; this.LblState.Content = state; this.LblLatitude.Content = latitude.ToString(); this.LblLongitude.Content = longitude.ToString(); this.LblIP.Content = ipv4; }
主页:https://geoip-db.com/