Tcl
Tcl
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 Tcl Downloads
load ./chilkat.dll
set success 0
# This example requires the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.
set http [new_CkHttp]
# Lookup an IPv4 address: 149.250.207.170 (this was a randomly chosen address)
set jsonStr [CkHttp_quickGetStr $http "http://api.ipstack.com/149.250.207.170?access_key=YOUR_ACCESS_KEY"]
if {[CkHttp_get_LastMethodSuccess $http] == 0} then {
puts [CkHttp_lastErrorText $http]
delete_CkHttp $http
exit
}
set json [new_CkJsonObject]
CkJsonObject_put_EmitCompact $json 0
set success [CkJsonObject_Load $json $jsonStr]
puts [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
# }
# }
set ip [CkJsonObject_stringOf $json "ip"]
set v_type [CkJsonObject_stringOf $json "type"]
set continent_code [CkJsonObject_stringOf $json "continent_code"]
set continent_name [CkJsonObject_stringOf $json "continent_name"]
set country_code [CkJsonObject_stringOf $json "country_code"]
set country_name [CkJsonObject_stringOf $json "country_name"]
set region_code [CkJsonObject_stringOf $json "region_code"]
set region_name [CkJsonObject_stringOf $json "region_name"]
set city [CkJsonObject_stringOf $json "city"]
set zip [CkJsonObject_stringOf $json "zip"]
set latitude [CkJsonObject_stringOf $json "latitude"]
set longitude [CkJsonObject_stringOf $json "longitude"]
set locationGeoname_id [CkJsonObject_stringOf $json "location.geoname_id"]
set locationCapital [CkJsonObject_stringOf $json "location.capital"]
set locationCountry_flag [CkJsonObject_stringOf $json "location.country_flag"]
set locationCountry_flag_emoji [CkJsonObject_stringOf $json "location.country_flag_emoji"]
set locationCountry_flag_emoji_unicode [CkJsonObject_stringOf $json "location.country_flag_emoji_unicode"]
set locationCalling_code [CkJsonObject_stringOf $json "location.calling_code"]
set locationIs_eu [CkJsonObject_BoolOf $json "location.is_eu"]
set i 0
set count_i [CkJsonObject_SizeOfArray $json "location.languages"]
while {$i < $count_i} {
CkJsonObject_put_I $json $i
set code [CkJsonObject_stringOf $json "location.languages[i].code"]
set name [CkJsonObject_stringOf $json "location.languages[i].name"]
set native [CkJsonObject_stringOf $json "location.languages[i].native"]
set i [expr $i + 1]
}
delete_CkHttp $http
delete_CkJsonObject $json