Integrate userinfo.io
Add the javascript library to your page
Get the library on CDNJS or download it and insert the following code between the
<head>
and </head>
tags of your page:
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/userinfo/1.1.0/userinfo.min.js"></script>
You can also use it as an AMD module. More details are on Github.
If needed, you can directly call the API with a JSONP callback or a JSONP variable. See this gist for an example.
Get your user's info
Anywhere in your code, use the library to get the info you need:
<script type="text/javascript">
UserInfo.getInfo(function(data) {
// the "data" object contains the info
}, function(err) {
// the "err" object contains useful information in case of an error
});
</script>
The information retrieval is asynchronous so it will not block your javascript thread.
The "info" object
Here is a example of a response you can receive:
{
request_date: "2014-09-18T04:11:25.154Z",
ip_address: "192.77.237.95",
position: {
latitude: 37.7758,
longitude: -122.4128,
accuracy: 3 // This is the accuracy radius, in kilometers
},
continent: {
name: "North America",
code: "NA",
},
country: {
name: "United States",
code: "US",
},
city: {
name: "San Francisco",
code: "94103"
}
}
Be sure to check if the fields have a value: if we can't resolve an ip, we won't consider it as an error and will send back an object with
null
values.
Add the java library to your dependencies
Add the library to your pom or download it:
<dependency>
<groupId>io.userinfo</groupId>
<artifactId>userinfo-java</artifactId>
<version>1.1.0</version>
</dependency>
More details on Github.
Get your user's info
Anywhere in your code, use the library to get the info you need:
String ipAddress = "192.77.237.95"; // your user's IP
Info info = UserInfo.getInfo(ipAddress);
System.out.println(info);
To get the IP address of an incoming request on a servlet, you can use the method
javax.servlet.ServletRequest#getRemoteAddr
.
The "Info" object
Here is the API of the object you will receive:
-
Info.class
DateTime getRequestDate()
: returns the request dateDateTime getRequestDate()
: returns the request dateString getIpAddress()
: returns the IP addressPosition getPosition()
: returns the position of the userPlace getContinent()
: returns the place defining the continentPlace getCountry()
: returns the place defining the countryPlace getCity()
: returns the place defining the city
-
Position.class
Double getLatitude()
: the latitudeDouble getLongitude()
: the longitudeDouble getAccuracy()
: the accuracy (in km)
-
Place.class
String getName()
: the name of the place-
String getCode()
: the international code of the place (for a city, it will be the postal code)
Be sure to check if the results have a value: if we can't resolve an ip, we won't consider it as an error and will send back an object with
null
values.
Commands
You can easily issue a request to the REST API using curl
.
Get you own information:
curl https://api.userinfo.io/userinfos
Get information on a specific IP address:
curl https://api.userinfo.io/userinfos?ip_address=192.77.237.95
The "info" object
Here is a example of a response you can receive:
{
request_date: "2014-09-18T04:11:25.154Z",
ip_address: "192.77.237.95",
position: {
latitude: 37.7758,
longitude: -122.4128,
accuracy: 3 // This is the accuracy radius, in kilometers
},
continent: {
name: "North America",
code: "NA",
},
country: {
name: "United States",
code: "US",
},
city: {
name: "San Francisco",
code: "94103"
}
}
XML Formatting
You can also request an XML formatting of the response:
curl https://api.userinfo.io/userinfos.xml
or:
curl https://api.userinfo.io/userinfos.xml?ip_address=192.77.237.95
The response will then look like:
<userinfo>
<requestDate>2014-09-18T04:11:25.154Z</requestDate>
<ipAddress>192.77.237.95</ipAddress>
<position>
<latitude>37.7758</latitude>
<longitude>-122.4128</longitude>
<accuracy>3</accuracy> <!-- This is the accuracy radius, in kilometers -->
</position>
<continent>
<name>North America</name>
<code>NA</code>
</continent>
<country>
<name>United States</name>
<code>US</code>
</country>
<city>
<name>San Francisco</name>
<code>94103</code>
</city>
</userinfo>
Accuracy
The vendors we use at userinfo.io provide us with the best quality of information possible. However, since there is no official and complete database of IP geolocation, there is currently no way to get 100% accurate data.
Here an estimate of the geolocation accuracy:
- Continent: ~99.9%
- Country: ~95%
- City: ~50%
- Exact position (latitude/longitude): ~50%
Usually, those results are satisfying to get an idea of a user's origin. It should not be used if the location is sensitive since we can't be sure of the exact position of the end-user.
As a rule of thumb, the results are pretty accurate in the most developped countries.
Support
If you have any problem with the service, drop us a line at hello@userinfo.io