To localize IP, you may use special API, available at:
http://api.ip2geo.pl/[FORMAT]/?ip=[IP]
API is also available via HTTPS.
[FORMAT]
parameter defines requested response type - it must be one of:
[CORS] Method allows for remote AJAX calls using CORS.
[1] Default variable name is dane
. You may change it using parameter var
,
e.g. http://api.ip2geo.pl/js/?var=newName
.
Name must match regexp: ^[$a-zA-Z_][0-9a-zA-Z_$]{0,19}$
[2] Default function name is callback
. You may change it using parameter jsonp
,
e.g. http://api.ip2geo.pl/jsonp/?jsonp=newName
.
Name must match regexp: ^[$a-zA-Z_][0-9a-zA-Z_$]{0,19}$
[IP]
parameter should contain IPv4 address to localize.
If ommited or erroneous, client's IP address is used.
API usually returns HTTP code 200. Any other response code must be considered an error - in such a case response body may not conform to format specified below.
Correct response consists of one or more UTF-8 encoded fields from the following list:
If IP was not present in any of the databases, an empty array is returned (in case of XML format - childless root element).
In case of error, response consists of the following fields:
Known error codes:
Possible responses:
Correct response:
a:4:{s:7:"country";s:2:"PL";s:4:"city";s:6:"Krakow";s:3:"lat";d:50.0833;s:3:"lon";d:19.9167;}
Empty response:
a:0:{}
Error:
a:2:{s:7:"errcode";i:0;s:3:"err";s:16:"Unknown error";}
Sample code presenting user's country (PHP):
<?php $dane = unserialize( file_get_contents( 'http://api.ip2geo.pl/php/?ip='.$_SERVER['REMOTE_ADDR'] ) ); if(isset($dane['country'])) { echo 'Country: '.$dane['country']."\n"; } ?>
Possible responses:
Correct response:
<ip2geo> <country>PL</country> <city>Krakow</city> <lat>50.0833</lon> <lon>19.9167</lat> </ip2geo>
Empty response:
<ip2geo/>
Error:
<ip2geo> <errcode>0</errcode> <err>Unknown error</err> </ip2geo>
Sample code presenting user's country (PHP):
<?php $data = file_get_contents( 'http://api.ip2geo.pl/xml/?ip='.$_SERVER['REMOTE_ADDR'] ); $prevState = libxml_disable_entity_loader(TRUE); $data = simplexml_load_string($data); libxml_disable_entity_loader($prevState); if($data->country) { echo 'Country: '.$data->country."\n"; } ?>
Sample code presenting user's country (JavaScript):
<p>Country: <span id="country"></span></p> <script type="text/javascript"> var xhr = new XMLHttpRequest(); xhr.open('GET', 'http://api.ip2geo.pl/xml/', true); xhr.onreadystatechange = function (event) { if (xhr.readyState == 4 && xhr.status == 200 && xhr.responseXML) { var location = xhr.responseXML.getElementsByTagName('country'); if(location.length > 0) { document.getElementById('country').appendChild( document.createTextNode(location[0].textContent) ); } } }; xhr.send(); </script>
Possible responses:
var
Correct response:
var dane = {"db":"MaxMind","country":"PL","city":"Krakow","lat":"50.0833","lon":"19.9167"};
Empty response:
var dane = [];
Error:
var dane = {"errcode":"0","err":"Unknown error"};
Sample code presenting user's country (JavaScript):
<p>Country: <span id="country"></span></p> <script type="text/javascript" src="http://api.ip2geo.pl/js/?var=data"></script> <script type="text/javascript"> if(data['country']) { document.getElementById('country').appendChild( document.createTextNode(data['country']) ); } </script>
Possible responses:
Correct response:
{"country":"PL","city":"Krakow","lat":50.0833,"lon":19.9167}
Empty response:
[]
Error:
{"errcode":0,"err":"Unknown error"}
Sample code presenting user's country (PHP):
<?php $data = json_decode( file_get_contents( 'http://api.ip2geo.pl/json/?ip='.$_SERVER['REMOTE_ADDR'] ) ); if(isset($data->country)) { echo 'Country: '.$data->country."\n"; } ?>
Sample code presenting user's country (JavaScript):
<p>Country: <span id="country"></span></p> <script type="text/javascript"> var xhr = new XMLHttpRequest(); xhr.open('GET', 'http://api.ip2geo.pl/json/', true); xhr.onreadystatechange = function (event) { if (xhr.readyState == 4 && xhr.status == 200 && xhr.response) { var location = JSON.parse(xhr.responseText); if(location.country) { document.getElementById('country').appendChild( document.createTextNode(location.country) ); } } }; xhr.send(); </script>
Possible responses:
jsonp
Correct response:
callback({"country":"PL","city":"Krakow","lat":50.0833,"lon":19.9167});
Empty response:
callback([]);
Error:
callback({"errcode":0,"err":"Unknown error"});
Sample code presenting user's country (JavaScript):
<p>Country: <span id="country"></span></p> <script type="text/javascript"> function callback_ip2geo(data) { if(data['country']) { document.getElementById('country').appendChild( document.createTextNode(data['country']) ); } } </script> <script type="text/javascript" src="http://api.ip2geo.pl/jsonp/?jsonp=callback_ip2geo"></script>
Possible responses:
Correct response:
country|PL city|Krakow lat|50.0833 lon|19.9167
Empty response:
Error:
errcode|0 err|Unknown error
It is forbidden to:
We reserve the right to limit the number of requests served to 1,000 per day per IP.
© 2015 by Jacekk.net