geo based IP lookup with ColdFusion
I've been looking around for a bit for a free location lookup based on an IP address. Preferably as an API call so I don't need to install/add too much my end.
I've come across hostip.info. This service is a community-based project with around 8,542,587 entries in the database!
They have a good API which supplies text and XML results, and they even have the database freely available for download with daily updates.
Below is a quick example of some code to get the country (you can find more examples at the site). If you want to see it in action click here.
1 <cfhttp url="http://api.hostip.info/?ip=#cgi.remote_host#" method="get" result="geoInfo" />
2
3 <cfscript>
4 geoXml = xmlParse(geoInfo.fileContent);
5 country = geoXml.HostipLookupResultSet['gml:featureMember']['hostip']['countryname'].xmlText;
6 countryAbbrev = geoXml.HostipLookupResultSet['gml:featureMember']['hostip']['countryAbbrev'].xmlText;
7 </cfscript>
8
9 <cfoutput>Your IP address is: #cgi.remote_host#<br/>
10 Your country is: #country# (#countryAbbrev#)
11 </cfoutput>
12 <hr/>
13 Full dump
14 <cfdump var="#geoXml#" />
For more info/help see also:
2
3 <cfscript>
4 geoXml = xmlParse(geoInfo.fileContent);
5 country = geoXml.HostipLookupResultSet['gml:featureMember']['hostip']['countryname'].xmlText;
6 countryAbbrev = geoXml.HostipLookupResultSet['gml:featureMember']['hostip']['countryAbbrev'].xmlText;
7 </cfscript>
8
9 <cfoutput>Your IP address is: #cgi.remote_host#<br/>
10 Your country is: #country# (#countryAbbrev#)
11 </cfoutput>
12 <hr/>
13 Full dump
14 <cfdump var="#geoXml#" />
TweetBacks
If you don't mind, I translated it to Portuguese and posted in our Brazilian blog http://porqueCF.com.br/blog (that means: why CF?). Of course I gave you all the credits and set links to your blog. Let me know if that was ok.
I also added yours to my "preferred blogs " session in http://ricardo.parente.us.
Thanks.
Ricardo