Sample code for 30+ languages & platforms
Unicode C

MaxMind IPv4 Geolocation Lookup

See more Geolocation Examples

Demonstrates how to lookup Geolocation data for an IPv4 address using the MaxMind GeoIP2 Precision Web Service.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkHttpW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttpW http;
    const wchar_t *jsonStr;
    HCkJsonObjectW json;
    const wchar_t *continentCode;
    int continentGeoname_id;
    const wchar_t *continentNamesJa;
    const wchar_t *continentNamesPt_BR;
    const wchar_t *continentNamesRu;
    const wchar_t *continentNamesZh_CN;
    const wchar_t *continentNamesDe;
    const wchar_t *continentNamesEn;
    const wchar_t *continentNamesEs;
    const wchar_t *continentNamesFr;
    BOOL countryIs_in_european_union;
    const wchar_t *countryIso_code;
    int countryGeoname_id;
    const wchar_t *countryNamesFr;
    const wchar_t *countryNamesJa;
    const wchar_t *countryNamesPt_BR;
    const wchar_t *countryNamesRu;
    const wchar_t *countryNamesZh_CN;
    const wchar_t *countryNamesDe;
    const wchar_t *countryNamesEn;
    const wchar_t *countryNamesEs;
    int maxmindQueries_remaining;
    BOOL registered_countryIs_in_european_union;
    const wchar_t *registered_countryIso_code;
    int registered_countryGeoname_id;
    const wchar_t *registered_countryNamesEs;
    const wchar_t *registered_countryNamesFr;
    const wchar_t *registered_countryNamesJa;
    const wchar_t *registered_countryNamesPt_BR;
    const wchar_t *registered_countryNamesRu;
    const wchar_t *registered_countryNamesZh_CN;
    const wchar_t *registered_countryNamesDe;
    const wchar_t *registered_countryNamesEn;
    const wchar_t *traitsIp_address;

    success = FALSE;

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    http = CkHttpW_Create();

    CkHttpW_putLogin(http,L"MAXMIND_ACCOUNT_ID");
    CkHttpW_putPassword(http,L"MAXMIND_LICENSE_KEY");
    CkHttpW_putAccept(http,L"application/json");

    // Lookup an IPv4 address: 149.250.207.170  (this was a randomly chosen address)
    jsonStr = CkHttpW_quickGetStr(http,L"https://geoip.maxmind.com/geoip/v2.1/country/149.250.207.170");
    if (CkHttpW_getLastMethodSuccess(http) == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        return;
    }

    json = CkJsonObjectW_Create();
    CkJsonObjectW_putEmitCompact(json,FALSE);
    success = CkJsonObjectW_Load(json,jsonStr);

    wprintf(L"%s\n",CkJsonObjectW_emit(json));

    // Sample output:
    // Use this online tool to generate parsing code from sample JSON: 
    // Generate Parsing Code from JSON

    // {
    //   "continent": {
    //     "code": "EU",
    //     "geoname_id": 6255148,
    //     "names": {
    //       "ja": "?????",
    //       "pt-BR": "Europa",
    //       "ru": "??????",
    //       "zh-CN": "??",
    //       "de": "Europa",
    //       "en": "Europe",
    //       "es": "Europa",
    //       "fr": "Europe"
    //     }
    //   },
    //   "country": {
    //     "is_in_european_union": true,
    //     "iso_code": "DE",
    //     "geoname_id": 2921044,
    //     "names": {
    //       "fr": "Allemagne",
    //       "ja": "????????",
    //       "pt-BR": "Alemanha",
    //       "ru": "????????",
    //       "zh-CN": "??",
    //       "de": "Deutschland",
    //       "en": "Germany",
    //       "es": "Alemania"
    //     }
    //   },
    //   "maxmind": {
    //     "queries_remaining": 49999
    //   },
    //   "registered_country": {
    //     "is_in_european_union": true,
    //     "iso_code": "DE",
    //     "geoname_id": 2921044,
    //     "names": {
    //       "es": "Alemania",
    //       "fr": "Allemagne",
    //       "ja": "????????",
    //       "pt-BR": "Alemanha",
    //       "ru": "????????",
    //       "zh-CN": "??",
    //       "de": "Deutschland",
    //       "en": "Germany"
    //     }
    //   },
    //   "traits": {
    //     "ip_address": "149.250.207.170"
    //   }
    // }
    // 
    // 

    continentCode = CkJsonObjectW_stringOf(json,L"continent.code");
    continentGeoname_id = CkJsonObjectW_IntOf(json,L"continent.geoname_id");
    continentNamesJa = CkJsonObjectW_stringOf(json,L"continent.names.ja");
    continentNamesPt_BR = CkJsonObjectW_stringOf(json,L"continent.names.pt-BR");
    continentNamesRu = CkJsonObjectW_stringOf(json,L"continent.names.ru");
    continentNamesZh_CN = CkJsonObjectW_stringOf(json,L"continent.names.zh-CN");
    continentNamesDe = CkJsonObjectW_stringOf(json,L"continent.names.de");
    continentNamesEn = CkJsonObjectW_stringOf(json,L"continent.names.en");
    continentNamesEs = CkJsonObjectW_stringOf(json,L"continent.names.es");
    continentNamesFr = CkJsonObjectW_stringOf(json,L"continent.names.fr");
    countryIs_in_european_union = CkJsonObjectW_BoolOf(json,L"country.is_in_european_union");
    countryIso_code = CkJsonObjectW_stringOf(json,L"country.iso_code");
    countryGeoname_id = CkJsonObjectW_IntOf(json,L"country.geoname_id");
    countryNamesFr = CkJsonObjectW_stringOf(json,L"country.names.fr");
    countryNamesJa = CkJsonObjectW_stringOf(json,L"country.names.ja");
    countryNamesPt_BR = CkJsonObjectW_stringOf(json,L"country.names.pt-BR");
    countryNamesRu = CkJsonObjectW_stringOf(json,L"country.names.ru");
    countryNamesZh_CN = CkJsonObjectW_stringOf(json,L"country.names.zh-CN");
    countryNamesDe = CkJsonObjectW_stringOf(json,L"country.names.de");
    countryNamesEn = CkJsonObjectW_stringOf(json,L"country.names.en");
    countryNamesEs = CkJsonObjectW_stringOf(json,L"country.names.es");
    maxmindQueries_remaining = CkJsonObjectW_IntOf(json,L"maxmind.queries_remaining");
    registered_countryIs_in_european_union = CkJsonObjectW_BoolOf(json,L"registered_country.is_in_european_union");
    registered_countryIso_code = CkJsonObjectW_stringOf(json,L"registered_country.iso_code");
    registered_countryGeoname_id = CkJsonObjectW_IntOf(json,L"registered_country.geoname_id");
    registered_countryNamesEs = CkJsonObjectW_stringOf(json,L"registered_country.names.es");
    registered_countryNamesFr = CkJsonObjectW_stringOf(json,L"registered_country.names.fr");
    registered_countryNamesJa = CkJsonObjectW_stringOf(json,L"registered_country.names.ja");
    registered_countryNamesPt_BR = CkJsonObjectW_stringOf(json,L"registered_country.names.pt-BR");
    registered_countryNamesRu = CkJsonObjectW_stringOf(json,L"registered_country.names.ru");
    registered_countryNamesZh_CN = CkJsonObjectW_stringOf(json,L"registered_country.names.zh-CN");
    registered_countryNamesDe = CkJsonObjectW_stringOf(json,L"registered_country.names.de");
    registered_countryNamesEn = CkJsonObjectW_stringOf(json,L"registered_country.names.en");
    traitsIp_address = CkJsonObjectW_stringOf(json,L"traits.ip_address");


    CkHttpW_Dispose(http);
    CkJsonObjectW_Dispose(json);

    }