Sample code for 30+ languages & platforms
Unicode C

ip2location.io GeoLocation API

See more Geolocation Examples

Demonstrates how to lookup Geolocation data for an IPv4 address using the ip2location.io GeoLocation 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 *country_code;
    const wchar_t *country_name;
    const wchar_t *region_name;
    const wchar_t *city_name;
    const wchar_t *latitude;
    const wchar_t *longitude;
    const wchar_t *zip_code;
    const wchar_t *time_zone;
    const wchar_t *asn;
    const wchar_t *v_as;
    BOOL is_proxy;

    success = FALSE;

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

    http = CkHttpW_Create();

    // Note: This is not a real API key.  Replace with your own...
    CkHttpW_SetUrlVar(http,L"api_key",L"2C312FBC9E667E5A0211F5152E5A1333");
    CkHttpW_SetUrlVar(http,L"ip_address",L"8.8.8.8");

    // Note: When first creating an ip2location.io account, make sure to at least subscribe to the free access.
    // Otherwise your API key will not yet work..
    jsonStr = CkHttpW_quickGetStr(http,L"https://api.ip2location.io/?key={$api_key}&ip={$ip_address}&format=json");
    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": "8.8.8.8",
    //   "country_code": "US",
    //   "country_name": "United States of America",
    //   "region_name": "California",
    //   "city_name": "Mountain View",
    //   "latitude": 37.405992,
    //   "longitude": -122.078515,
    //   "zip_code": "94043",
    //   "time_zone": "-07:00",
    //   "asn": "15169",
    //   "as": "Google LLC",
    //   "is_proxy": false
    // }

    ip = CkJsonObjectW_stringOf(json,L"ip");
    country_code = CkJsonObjectW_stringOf(json,L"country_code");
    country_name = CkJsonObjectW_stringOf(json,L"country_name");
    region_name = CkJsonObjectW_stringOf(json,L"region_name");
    city_name = CkJsonObjectW_stringOf(json,L"city_name");
    latitude = CkJsonObjectW_stringOf(json,L"latitude");
    longitude = CkJsonObjectW_stringOf(json,L"longitude");
    zip_code = CkJsonObjectW_stringOf(json,L"zip_code");
    time_zone = CkJsonObjectW_stringOf(json,L"time_zone");
    asn = CkJsonObjectW_stringOf(json,L"asn");
    v_as = CkJsonObjectW_stringOf(json,L"as");
    is_proxy = CkJsonObjectW_BoolOf(json,L"is_proxy");


    CkHttpW_Dispose(http);
    CkJsonObjectW_Dispose(json);

    }