Application Programming Interface

Contents

  1. General information
  2. Parameters
    1. Format
    2. IP
  3. Responses
    1. Correct response
    2. Empty response
    3. Error
  4. Examples
    1. PHP
    2. XML
    3. JS
    4. JSON
    5. JSONP
    6. TXT
  5. Regulations

General information

To localize IP, you may use special API, available at:

http://api.ip2geo.pl/[FORMAT]/?ip=[IP]

API is also available via HTTPS.

Parameters

Format

[FORMAT] parameter defines requested response type - it must be one of:

php
serialized PHP associative array
xml
XML document [CORS]
js
JavaScript defining global variable of given name[1]
json
associative array in JavaScript Object Notation [CORS]
jsonp
JavaScript calling function of given name[2] with location as an argument
txt
text file, each line is in form of: key|value

[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

[IP] parameter should contain IPv4 address to localize. If ommited or erroneous, client's IP address is used.

Responses

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

Correct response consists of one or more UTF-8 encoded fields from the following list:

db (string)
name of the source of the record (ie. MaxMind, ip2geo.pl)
country (string)
two-letter country code, per ISO 3166-1 alpha-2
city (string)
city name
lat (float)
latitude - from -90 to 0 for latitudes on Southern Hemisphere or from 0 to 90 for ones on Northern Hemisphere
lon (float)
longitude - from -180 to 0 for longitudes on Western Hemisphere or from 0 to 180 for ones on Eastern Hemisphere
proxy (bool)
present, when IP belongs to a known proxy server
satellite (bool)
present, when IP belongs to satellite connection providers

Empty response

If IP was not present in any of the databases, an empty array is returned (in case of XML format - childless root element).

Error

In case of error, response consists of the following fields:

errcode (int)
Error code
err (string)
Error message (may be in Polish!)

Known error codes:

0
Unspecified error
477
Provided IP is a multicast address
478
Provided IP is an address reserved by IANA
479
Provided IP is a private address
503
API limits exceeded

Examples

PHP

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):

Warning! The following code may be insecure! In production systems use code provided in JSON API example.
<?php
$dane = unserialize(
    file_get_contents(
        'http://api.ip2geo.pl/php/?ip='.$_SERVER['REMOTE_ADDR']
    )
);
if(isset($dane['country'])) {
    echo 'Country: '.$dane['country']."\n";
}
?>

XML

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>

JS

Possible responses:

Information: name of the variable may be specified using parameter 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>

JSON

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>

JSONP

Possible responses:

Information: name of the callback function may be specified using parameter 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>

TXT

Possible responses:

Correct response:

country|PL
city|Krakow
lat|50.0833
lon|19.9167

Empty response:


Error:

errcode|0
err|Unknown error

Regulations

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