Lua
Lua
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 Lua Downloads
local chilkat = require("chilkat")
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local http = chilkat.Http.new()
-- Lookup an IPv4 address: 149.250.207.170 (this was a randomly chosen address)
local jsonStr = http:quick_get_str("http://api.ipstack.com/149.250.207.170?access_key=YOUR_ACCESS_KEY")
if not http.last_method_success then
print(http.last_error_text)
return
end
local json = chilkat.JsonObject.new()
json.emit_compact = false
local success = json:load(jsonStr)
print(json:emit())
-- 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
-- }
-- }
local code = ""
local name = ""
local native = ""
local ip = json:string_of("ip")
local v_type = json:string_of("type")
local continent_code = json:string_of("continent_code")
local continent_name = json:string_of("continent_name")
local country_code = json:string_of("country_code")
local country_name = json:string_of("country_name")
local region_code = json:string_of("region_code")
local region_name = json:string_of("region_name")
local city = json:string_of("city")
local zip = json:string_of("zip")
local latitude = json:string_of("latitude")
local longitude = json:string_of("longitude")
local locationGeoname_id = json:string_of("location.geoname_id")
local locationCapital = json:string_of("location.capital")
local locationCountry_flag = json:string_of("location.country_flag")
local locationCountry_flag_emoji = json:string_of("location.country_flag_emoji")
local locationCountry_flag_emoji_unicode = json:string_of("location.country_flag_emoji_unicode")
local locationCalling_code = json:string_of("location.calling_code")
local locationIs_eu = json:bool_of("location.is_eu")
local i = 0
local count_i = json:size_of_array("location.languages")
while i < count_i do
json.i = i
code = json:string_of("location.languages[i].code")
name = json:string_of("location.languages[i].name")
native = json:string_of("location.languages[i].native")
i = i + 1
end