Free Geolocation API tool

NOTE: have done away with the free api access from their site. So the following sample code will no longer work. They now provide a free sample database on their site and also a complete paid version. offers a free geolocation API that lets you query with an ip address and get the location details such as city, country, zip, latitude, longitude etc. The site also offers a free MySQL database for the same if you would like to install it on your server. I’ve used CURL to wrap the API access. The complete function with a sample query and the response is shown below.

    * Geolocation API access
    * @param    string  $ip         IP address to query
    * @param    string  $format     output format of response
    * @return   string  XML, JSON or CSV string
    function get_ip_location($ip, $format="xml") {
        /* Set allowed output formats */
        $formats_allowed = array("json", "xml", "raw");
        /* IP location query url */
        $query_url = "";
        /* Male sure that the format is one of json, xml, raw.
           Or else default to xml */
        if(!in_array($format, $formats_allowed)) {
            $format = "xml";
        $query_url = $query_url . "{$ip}&output={$format}";
        /* Init CURL and its options*/
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $query_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);
        /* Execute CURL and get the response */
        return curl_exec($ch);

    $location_data = get_ip_location("");


Query response:
	United States
	Mountain View

The function by default returns a XML response, but you can also get a response in json or csv format, as shown below.

/* Get response in 'json' format' */
$location_data = get_ip_location("", "json");
"Ip" : "",
"Status" : "OK",
"CountryCode" : "US",
"CountryName" : "United States",
"RegionCode" : "06",
"RegionName" : "California",
"City" : "Mountain View",
"ZipPostalCode" : "94043",
"Latitude" : "37.4192",
"Longitude" : "-122.057",


/* Get response in 'csv' format' */
$location_data = get_ip_location("", "raw");,OK,US,United States,06,California,
Mountain View,94043,37.4192,-122.057

You can further parse the returned XML using SimpleXML.

    $location_data = get_ip_location("");
    $data = '';
    try {
        $data = @new SimpleXMLElement($location_data);
    catch(Exception $e) {
        echo "Error parsing XML.";
    foreach($data as $key=>$value)
        echo $key . " : " . $value . "\n";


14 thoughts to “Free Geolocation API tool”

  1. I’m not sure what the use is of returning a plaintext format? If you want to fetch the information from the webservice why not output it in a structure that can be used in php.

  2. The CSV format can make it easier to save the data to a file, which can later be opened in Excel.

  3. it doesn’t work…..
    error occurs….
    Fatal error: Call to undefined function curl_init() in F:\www\iplocation\iplocation.php on line 28

  4. CURL is not enabled on your server. If you are running on your localhost enable CURL in your php.ini,



    you could use the ‘file_get_contents’ function in place of CURL in the ‘get_ip_location’ function above:

    $query_url = $query_url . “{$ip}&output={$format}”;

    $data = file_get_contents($query_url);

    if (!$data)
    return false;
    return $data;

  5. what if you want to use only the country name and city from the xml, how will the code change ?

  6. Instead of the foreach loop, use the following:

    $country = (string) $data->CountryName;
    $city = (string) $data->City;

  7. hai,
    It’s nice.We can find the geo location of an ip-address using ip-details.It’s used to Search and find ip address’s,domain name host’s whois information including city, country, global latitude & longitude’s useful for me.

  8. Harms……Thanks a ton ! I was searching for the changed url but I could’nt find it anywhere. My search ends here. Thanks Again

Leave a Reply

Your email address will not be published.