geoPlugin Home

JSON Web Service

The JSON web service 1) allows you to directly program your back-end JSON scripts to deliver dynamic geo-localized pages using the JSON variables provided by geoPlugin.

To access this service, add the following url to a remote include call


http://www.geoplugin.net/json.gp?ip=xx.xx.xx.xx


Of course, substitute the xx's with your visitor's IP number. If you are calling the JSON service via jQuery, you don't need to include the IP since the IP will be automatically resolved to your visitor.

Example output of a JSON query for your IP address (54.161.168.21) is:

{
	
  "geoplugin_request":"54.161.168.21",
	
  "geoplugin_status":200,
  "geoplugin_credit":"Some of the returned data includes GeoLite data created by MaxMind, available from http:\/\/www.maxmind.com<\/a>.",
	
  "geoplugin_city":"Seattle",
	
  "geoplugin_region":"WA",
	
  "geoplugin_areaCode":"206",
	
  "geoplugin_dmaCode":"819",
	
  "geoplugin_countryCode":"US",
	
  "geoplugin_countryName":"United States",
	
  "geoplugin_continentCode":"NA",
	
  "geoplugin_latitude":"47.6344",
	
  "geoplugin_longitude":"-122.3422",
	
  "geoplugin_regionCode":"WA",
	
  "geoplugin_regionName":"Washington",
	
  "geoplugin_currencyCode":"USD",
	
  "geoplugin_currencySymbol":"$",
	
  "geoplugin_currencySymbol_UTF8":"$",
	
  "geoplugin_currencyConverter":1

}




JSON Currency Converter

The variable “geoplugin_currencyConverter” is the conversion rate for the currency converter base currency.
Like all calls to any of geoPlugin's web services, the default base_currency is USD ($US).
Thus, if your base currency is NOT $US, then you must add the variable base_currency=XXX to the call to geoplugin.net
eg

http://www.geoplugin.net/json.gp?base_currency=EUR

Now geoplugin_currencyConverter will output the exchange rate of one Euro for your visitor.

The base_currency value must be a valid ISO 4217 3-letter code.


AJAX and Error: Invalid label

If you are using jQuery for example to do AJAX calls to the JSON webservice, you will probably be seeing the Javascript error Error: Invalid label


This is due to data requests from a server (geoPlugin) in a different domain (your web server). To eliminate this problem, JSONP or “JSON with padding” is required.
To return the JSON results as JSONP, tag jsoncallback=? onto the url when making the jQuery Ajax call to any JSON webservice
eg

 
$.getJSON("http://www.geoplugin.net/json.gp?jsoncallback=?",
function (data) {
	for (var i in data) {
		document.write('data["i"] = ' + i + '<br/>');
	}
);



An AJAX self-contained currency converter

Here is a working, extremely simple example of how to use the JSON service.
Here, we create a live AJAX currency converter, with little more than a few lines of HTML.

 
<script src="http://www.google.com/jsapi"></script>
<script src="http://www.geoplugin.net/javascript.gp" type="text/javascript"></script>
<script src="http://www.geoplugin.net/ajax_currency_converter.gp"></script>
Convert 
<input type='text' id='gp_amount' size='4' /> 
<select id="gp_from"></select> 
to 
<select id="gp_to"></select>
<p><input type='button' onClick='gp_convertIt()' value = 'Convert It' /></p>
<script>gp_currencySymbols()</script>
 
<div id="gp_converted"></div>


which will output:

Convert to




See also

1) This product includes GeoLite data created by MaxMind, available from http://maxmind.com/
 
 
Bookmark with:    Digg    StumbleUpon    Del.icio.us    Technorati


Acceptable Use Policy    Contact geoPlugin