C
C
ipdata.co IPv4 Geolocation Lookup
See more Geolocation Examples
Demonstrates how to lookup Geolocation data for an IPv4 address using the ipdata.co API.Chilkat C Downloads
#include <C_CkHttp.h>
#include <C_CkJsonObject.h>
void ChilkatSample(void)
{
BOOL success;
HCkHttp http;
const char *jsonStr;
HCkJsonObject json;
const char *ip;
BOOL is_eu;
const char *city;
const char *region;
const char *region_code;
const char *country_name;
const char *country_code;
const char *continent_name;
const char *continent_code;
const char *latitude;
const char *longitude;
const char *asn;
const char *organisation;
const char *postal;
const char *calling_code;
const char *flag;
const char *emoji_flag;
const char *emoji_unicode;
const char *currencyName;
const char *currencyCode;
const char *currencySymbol;
const char *currencyNative;
const char *currencyPlural;
const char *time_zoneName;
const char *time_zoneAbbr;
const char *time_zoneOffset;
BOOL time_zoneIs_dst;
const char *time_zoneCurrent_time;
BOOL threatIs_tor;
BOOL threatIs_proxy;
BOOL threatIs_anonymous;
BOOL threatIs_known_attacker;
BOOL threatIs_known_abuser;
BOOL threatIs_threat;
BOOL threatIs_bogon;
const char *count;
int i;
int count_i;
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();
jsonStr = CkHttp_quickGetStr(http,"https://api.ipdata.co/149.250.207.170?api-key=MY_API_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",
// "is_eu": true,
// "city": null,
// "region": null,
// "region_code": null,
// "country_name": "Germany",
// "country_code": "DE",
// "continent_name": "Europe",
// "continent_code": "EU",
// "latitude": 51.2993,
// "longitude": 9.491,
// "asn": "AS15854",
// "organisation": "EntServ Deutschland GmbH",
// "postal": null,
// "calling_code": "49",
// "flag": "https://ipdata.co/flags/de.png",
// "emoji_flag": "\ud83c\udde9\ud83c\uddea",
// "emoji_unicode": "U+1F1E9 U+1F1EA",
// "languages": [
// {
// "name": "German",
// "native": "Deutsch"
// }
// ],
// "currency": {
// "name": "Euro",
// "code": "EUR",
// "symbol": "\u20ac",
// "native": "\u20ac",
// "plural": "euros"
// },
// "time_zone": {
// "name": "Europe/Berlin",
// "abbr": "CEST",
// "offset": "+0200",
// "is_dst": true,
// "current_time": "2019-04-20T23:54:30.715507+02:00"
// },
// "threat": {
// "is_tor": false,
// "is_proxy": false,
// "is_anonymous": false,
// "is_known_attacker": false,
// "is_known_abuser": false,
// "is_threat": false,
// "is_bogon": false
// },
// "count": "2"
// }
ip = CkJsonObject_stringOf(json,"ip");
is_eu = CkJsonObject_BoolOf(json,"is_eu");
city = CkJsonObject_stringOf(json,"city");
region = CkJsonObject_stringOf(json,"region");
region_code = CkJsonObject_stringOf(json,"region_code");
country_name = CkJsonObject_stringOf(json,"country_name");
country_code = CkJsonObject_stringOf(json,"country_code");
continent_name = CkJsonObject_stringOf(json,"continent_name");
continent_code = CkJsonObject_stringOf(json,"continent_code");
latitude = CkJsonObject_stringOf(json,"latitude");
longitude = CkJsonObject_stringOf(json,"longitude");
asn = CkJsonObject_stringOf(json,"asn");
organisation = CkJsonObject_stringOf(json,"organisation");
postal = CkJsonObject_stringOf(json,"postal");
calling_code = CkJsonObject_stringOf(json,"calling_code");
flag = CkJsonObject_stringOf(json,"flag");
emoji_flag = CkJsonObject_stringOf(json,"emoji_flag");
emoji_unicode = CkJsonObject_stringOf(json,"emoji_unicode");
currencyName = CkJsonObject_stringOf(json,"currency.name");
currencyCode = CkJsonObject_stringOf(json,"currency.code");
currencySymbol = CkJsonObject_stringOf(json,"currency.symbol");
currencyNative = CkJsonObject_stringOf(json,"currency.native");
currencyPlural = CkJsonObject_stringOf(json,"currency.plural");
time_zoneName = CkJsonObject_stringOf(json,"time_zone.name");
time_zoneAbbr = CkJsonObject_stringOf(json,"time_zone.abbr");
time_zoneOffset = CkJsonObject_stringOf(json,"time_zone.offset");
time_zoneIs_dst = CkJsonObject_BoolOf(json,"time_zone.is_dst");
time_zoneCurrent_time = CkJsonObject_stringOf(json,"time_zone.current_time");
threatIs_tor = CkJsonObject_BoolOf(json,"threat.is_tor");
threatIs_proxy = CkJsonObject_BoolOf(json,"threat.is_proxy");
threatIs_anonymous = CkJsonObject_BoolOf(json,"threat.is_anonymous");
threatIs_known_attacker = CkJsonObject_BoolOf(json,"threat.is_known_attacker");
threatIs_known_abuser = CkJsonObject_BoolOf(json,"threat.is_known_abuser");
threatIs_threat = CkJsonObject_BoolOf(json,"threat.is_threat");
threatIs_bogon = CkJsonObject_BoolOf(json,"threat.is_bogon");
count = CkJsonObject_stringOf(json,"count");
i = 0;
count_i = CkJsonObject_SizeOfArray(json,"languages");
while (i < count_i) {
CkJsonObject_putI(json,i);
name = CkJsonObject_stringOf(json,"languages[i].name");
native = CkJsonObject_stringOf(json,"languages[i].native");
i = i + 1;
}
CkHttp_Dispose(http);
CkJsonObject_Dispose(json);
}