Sample code for 30+ languages & platforms
C

ipinfo.io IPv4 Geolocation Lookup

See more Geolocation Examples

Demonstrates how to lookup Geolocation data for an IPv4 address using the ipinfo.io 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 *hostname;
    const char *city;
    const char *region;
    const char *country;
    const char *loc;
    const char *postal;
    const char *asnAsn;
    const char *asnName;
    const char *asnDomain;
    const char *asnRoute;
    const char *asnType;
    const char *companyName;
    const char *companyDomain;
    const char *companyType;
    const char *carrierName;
    const char *carrierMcc;
    const char *carrierMnc;

    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,"ACCESS_TOKEN");
    CkHttp_putPassword(http,"");
    CkHttp_putAccept(http,"application/json");

    // Lookup an IPv4 address: 149.250.207.170  (this was a randomly chosen address)
    jsonStr = CkHttp_quickGetStr(http,"https://ipinfo.io/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

    // Note: This is the output for the free trial account.
    // 
    // {
    //   "ip": "149.250.207.170",
    //   "city": "",
    //   "region": "",
    //   "country": "DE",
    //   "loc": "51.2993,9.4910",
    //   "org": "AS15854 EntServ Deutschland GmbH"
    // }

    // A full response would look like this:

    // {
    //   "ip": "66.87.125.72",
    //   "hostname": "66-87-125-72.pools.spcsdns.net",
    //   "city": "Southbridge",
    //   "region": "Massachusetts",
    //   "country": "US",
    //   "loc": "42.0707,-72.0440",
    //   "postal": "01550",
    //   "asn": {
    //     "asn": "AS10507",
    //     "name": "Sprint Personal Communications Systems",
    //     "domain": "spcsdns.net",
    //     "route": "66.87.125.0/24",
    //     "type": "isp"
    //   },
    //   "company": {
    //     "name": "Sprint Springfield POP",
    //     "domain": "sprint.com",
    //     "type": "isp"
    //   },  
    //   "carrier": {
    //     "name": "Sprint",
    //     "mcc": "310",
    //     "mnc": "120"
    //   }
    // }
    // 

    ip = CkJsonObject_stringOf(json,"ip");
    hostname = CkJsonObject_stringOf(json,"hostname");
    city = CkJsonObject_stringOf(json,"city");
    region = CkJsonObject_stringOf(json,"region");
    country = CkJsonObject_stringOf(json,"country");
    loc = CkJsonObject_stringOf(json,"loc");
    postal = CkJsonObject_stringOf(json,"postal");
    asnAsn = CkJsonObject_stringOf(json,"asn.asn");
    asnName = CkJsonObject_stringOf(json,"asn.name");
    asnDomain = CkJsonObject_stringOf(json,"asn.domain");
    asnRoute = CkJsonObject_stringOf(json,"asn.route");
    asnType = CkJsonObject_stringOf(json,"asn.type");
    companyName = CkJsonObject_stringOf(json,"company.name");
    companyDomain = CkJsonObject_stringOf(json,"company.domain");
    companyType = CkJsonObject_stringOf(json,"company.type");
    carrierName = CkJsonObject_stringOf(json,"carrier.name");
    carrierMcc = CkJsonObject_stringOf(json,"carrier.mcc");
    carrierMnc = CkJsonObject_stringOf(json,"carrier.mnc");


    CkHttp_Dispose(http);
    CkJsonObject_Dispose(json);

    }