Sample code for 30+ languages & platforms
Unicode C

ipstack.com IPv4 Geolocation Lookup

See more Geolocation Examples

Demonstrates how to lookup Geolocation data for an IPv4 address using the ipstack.com REST API.

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 *ip;
    const wchar_t *v_type;
    const wchar_t *continent_code;
    const wchar_t *continent_name;
    const wchar_t *country_code;
    const wchar_t *country_name;
    const wchar_t *region_code;
    const wchar_t *region_name;
    const wchar_t *city;
    const wchar_t *zip;
    const wchar_t *latitude;
    const wchar_t *longitude;
    const wchar_t *locationGeoname_id;
    const wchar_t *locationCapital;
    const wchar_t *locationCountry_flag;
    const wchar_t *locationCountry_flag_emoji;
    const wchar_t *locationCountry_flag_emoji_unicode;
    const wchar_t *locationCalling_code;
    BOOL locationIs_eu;
    int i;
    int count_i;
    const wchar_t *code;
    const wchar_t *name;
    const wchar_t *native;

    success = FALSE;

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

    http = CkHttpW_Create();

    //  Lookup an IPv4 address: 149.250.207.170  (this was a randomly chosen address)
    jsonStr = CkHttpW_quickGetStr(http,L"http://api.ipstack.com/149.250.207.170?access_key=YOUR_ACCESS_KEY");
    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

    //  {
    //    "ip": "149.250.207.170",
    //    "type": "ipv4",
    //    "continent_code": "EU",
    //    "continent_name": "Europe",
    //    "country_code": "DE",
    //    "country_name": "Germany",
    //    "region_code": null,
    //    "region_name": null,
    //    "city": null,
    //    "zip": null,
    //    "latitude": 51.2993,
    //    "longitude": 9.491,
    //    "location": {
    //      "geoname_id": null,
    //      "capital": "Berlin",
    //      "languages": [
    //        {
    //          "code": "de",
    //          "name": "German",
    //          "native": "Deutsch"
    //        }
    //      ],
    //      "country_flag": "http:\/\/assets.ipstack.com\/flags\/de.svg",
    //      "country_flag_emoji": "\ud83c\udde9\ud83c\uddea",
    //      "country_flag_emoji_unicode": "U+1F1E9 U+1F1EA",
    //      "calling_code": "49",
    //      "is_eu": true
    //    }
    //  }

    ip = CkJsonObjectW_stringOf(json,L"ip");
    v_type = CkJsonObjectW_stringOf(json,L"type");
    continent_code = CkJsonObjectW_stringOf(json,L"continent_code");
    continent_name = CkJsonObjectW_stringOf(json,L"continent_name");
    country_code = CkJsonObjectW_stringOf(json,L"country_code");
    country_name = CkJsonObjectW_stringOf(json,L"country_name");
    region_code = CkJsonObjectW_stringOf(json,L"region_code");
    region_name = CkJsonObjectW_stringOf(json,L"region_name");
    city = CkJsonObjectW_stringOf(json,L"city");
    zip = CkJsonObjectW_stringOf(json,L"zip");
    latitude = CkJsonObjectW_stringOf(json,L"latitude");
    longitude = CkJsonObjectW_stringOf(json,L"longitude");
    locationGeoname_id = CkJsonObjectW_stringOf(json,L"location.geoname_id");
    locationCapital = CkJsonObjectW_stringOf(json,L"location.capital");
    locationCountry_flag = CkJsonObjectW_stringOf(json,L"location.country_flag");
    locationCountry_flag_emoji = CkJsonObjectW_stringOf(json,L"location.country_flag_emoji");
    locationCountry_flag_emoji_unicode = CkJsonObjectW_stringOf(json,L"location.country_flag_emoji_unicode");
    locationCalling_code = CkJsonObjectW_stringOf(json,L"location.calling_code");
    locationIs_eu = CkJsonObjectW_BoolOf(json,L"location.is_eu");
    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(json,L"location.languages");
    while (i < count_i) {
        CkJsonObjectW_putI(json,i);
        code = CkJsonObjectW_stringOf(json,L"location.languages[i].code");
        name = CkJsonObjectW_stringOf(json,L"location.languages[i].name");
        native = CkJsonObjectW_stringOf(json,L"location.languages[i].native");
        i = i + 1;
    }



    CkHttpW_Dispose(http);
    CkJsonObjectW_Dispose(json);

    }