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

    success = FALSE;

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

    http = CkHttp_Create();

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

    //  {
    //    "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 = CkJsonObject_stringOf(json,"ip");
    v_type = CkJsonObject_stringOf(json,"type");
    continent_code = CkJsonObject_stringOf(json,"continent_code");
    continent_name = CkJsonObject_stringOf(json,"continent_name");
    country_code = CkJsonObject_stringOf(json,"country_code");
    country_name = CkJsonObject_stringOf(json,"country_name");
    region_code = CkJsonObject_stringOf(json,"region_code");
    region_name = CkJsonObject_stringOf(json,"region_name");
    city = CkJsonObject_stringOf(json,"city");
    zip = CkJsonObject_stringOf(json,"zip");
    latitude = CkJsonObject_stringOf(json,"latitude");
    longitude = CkJsonObject_stringOf(json,"longitude");
    locationGeoname_id = CkJsonObject_stringOf(json,"location.geoname_id");
    locationCapital = CkJsonObject_stringOf(json,"location.capital");
    locationCountry_flag = CkJsonObject_stringOf(json,"location.country_flag");
    locationCountry_flag_emoji = CkJsonObject_stringOf(json,"location.country_flag_emoji");
    locationCountry_flag_emoji_unicode = CkJsonObject_stringOf(json,"location.country_flag_emoji_unicode");
    locationCalling_code = CkJsonObject_stringOf(json,"location.calling_code");
    locationIs_eu = CkJsonObject_BoolOf(json,"location.is_eu");
    i = 0;
    count_i = CkJsonObject_SizeOfArray(json,"location.languages");
    while (i < count_i) {
        CkJsonObject_putI(json,i);
        code = CkJsonObject_stringOf(json,"location.languages[i].code");
        name = CkJsonObject_stringOf(json,"location.languages[i].name");
        native = CkJsonObject_stringOf(json,"location.languages[i].native");
        i = i + 1;
    }



    CkHttp_Dispose(http);
    CkJsonObject_Dispose(json);

    }