Sample code for 30+ languages & platforms
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 C Downloads

C
#include <C_CkHttp.h>
#include <C_CkJsonObject.h>

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

    success = FALSE;

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

    http = CkHttp_Create();

    CkHttp_putLogin(http,"MAXMIND_ACCOUNT_ID");
    CkHttp_putPassword(http,"MAXMIND_LICENSE_KEY");
    CkHttp_putAccept(http,"application/json");

    // Lookup an IPv4 address: 149.250.207.170  (this was a randomly chosen address)
    jsonStr = CkHttp_quickGetStr(http,"https://geoip.maxmind.com/geoip/v2.1/country/149.250.207.170");
    if (CkHttp_getLastMethodSuccess(http) == FALSE) {
        printf("%s\n",CkHttp_lastErrorText(http));
        CkHttp_Dispose(http);
        return;
    }

    json = CkJsonObject_Create();
    CkJsonObject_putEmitCompact(json,FALSE);
    success = CkJsonObject_Load(json,jsonStr);

    printf("%s\n",CkJsonObject_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 = CkJsonObject_stringOf(json,"continent.code");
    continentGeoname_id = CkJsonObject_IntOf(json,"continent.geoname_id");
    continentNamesJa = CkJsonObject_stringOf(json,"continent.names.ja");
    continentNamesPt_BR = CkJsonObject_stringOf(json,"continent.names.pt-BR");
    continentNamesRu = CkJsonObject_stringOf(json,"continent.names.ru");
    continentNamesZh_CN = CkJsonObject_stringOf(json,"continent.names.zh-CN");
    continentNamesDe = CkJsonObject_stringOf(json,"continent.names.de");
    continentNamesEn = CkJsonObject_stringOf(json,"continent.names.en");
    continentNamesEs = CkJsonObject_stringOf(json,"continent.names.es");
    continentNamesFr = CkJsonObject_stringOf(json,"continent.names.fr");
    countryIs_in_european_union = CkJsonObject_BoolOf(json,"country.is_in_european_union");
    countryIso_code = CkJsonObject_stringOf(json,"country.iso_code");
    countryGeoname_id = CkJsonObject_IntOf(json,"country.geoname_id");
    countryNamesFr = CkJsonObject_stringOf(json,"country.names.fr");
    countryNamesJa = CkJsonObject_stringOf(json,"country.names.ja");
    countryNamesPt_BR = CkJsonObject_stringOf(json,"country.names.pt-BR");
    countryNamesRu = CkJsonObject_stringOf(json,"country.names.ru");
    countryNamesZh_CN = CkJsonObject_stringOf(json,"country.names.zh-CN");
    countryNamesDe = CkJsonObject_stringOf(json,"country.names.de");
    countryNamesEn = CkJsonObject_stringOf(json,"country.names.en");
    countryNamesEs = CkJsonObject_stringOf(json,"country.names.es");
    maxmindQueries_remaining = CkJsonObject_IntOf(json,"maxmind.queries_remaining");
    registered_countryIs_in_european_union = CkJsonObject_BoolOf(json,"registered_country.is_in_european_union");
    registered_countryIso_code = CkJsonObject_stringOf(json,"registered_country.iso_code");
    registered_countryGeoname_id = CkJsonObject_IntOf(json,"registered_country.geoname_id");
    registered_countryNamesEs = CkJsonObject_stringOf(json,"registered_country.names.es");
    registered_countryNamesFr = CkJsonObject_stringOf(json,"registered_country.names.fr");
    registered_countryNamesJa = CkJsonObject_stringOf(json,"registered_country.names.ja");
    registered_countryNamesPt_BR = CkJsonObject_stringOf(json,"registered_country.names.pt-BR");
    registered_countryNamesRu = CkJsonObject_stringOf(json,"registered_country.names.ru");
    registered_countryNamesZh_CN = CkJsonObject_stringOf(json,"registered_country.names.zh-CN");
    registered_countryNamesDe = CkJsonObject_stringOf(json,"registered_country.names.de");
    registered_countryNamesEn = CkJsonObject_stringOf(json,"registered_country.names.en");
    traitsIp_address = CkJsonObject_stringOf(json,"traits.ip_address");


    CkHttp_Dispose(http);
    CkJsonObject_Dispose(json);

    }