Lua
Lua
MaxMind IPv4 Geolocation Lookup
See more Geolocation Examples
Demonstrates how to lookup Geolocation data for an IPv4 address using the MaxMind GeoIP2 Precision Web Service.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()
http.login = "MAXMIND_ACCOUNT_ID"
http.password = "MAXMIND_LICENSE_KEY"
http.accept = "application/json"
-- Lookup an IPv4 address: 149.250.207.170 (this was a randomly chosen address)
local jsonStr = http:quick_get_str("https://geoip.maxmind.com/geoip/v2.1/country/149.250.207.170")
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
-- {
-- "continent": {
-- "code": "EU",
-- "geoname_id": 6255148,
-- "names": {
-- "ja": "?????",
-- "pt-BR": "Europa",
-- "ru": "??????",
-- "zh-CN": "??",
-- "de": "Europa",
-- "en": "Europe",
-- "es": "Europa",
-- "fr": "Europe"
-- }
-- },
-- "country": {
-- "is_in_european_union": true,
-- "iso_code": "DE",
-- "geoname_id": 2921044,
-- "names": {
-- "fr": "Allemagne",
-- "ja": "????????",
-- "pt-BR": "Alemanha",
-- "ru": "????????",
-- "zh-CN": "??",
-- "de": "Deutschland",
-- "en": "Germany",
-- "es": "Alemania"
-- }
-- },
-- "maxmind": {
-- "queries_remaining": 49999
-- },
-- "registered_country": {
-- "is_in_european_union": true,
-- "iso_code": "DE",
-- "geoname_id": 2921044,
-- "names": {
-- "es": "Alemania",
-- "fr": "Allemagne",
-- "ja": "????????",
-- "pt-BR": "Alemanha",
-- "ru": "????????",
-- "zh-CN": "??",
-- "de": "Deutschland",
-- "en": "Germany"
-- }
-- },
-- "traits": {
-- "ip_address": "149.250.207.170"
-- }
-- }
--
--
local continentCode = json:string_of("continent.code")
local continentGeoname_id = json:int_of("continent.geoname_id")
local continentNamesJa = json:string_of("continent.names.ja")
local continentNamesPt_BR = json:string_of("continent.names.pt-BR")
local continentNamesRu = json:string_of("continent.names.ru")
local continentNamesZh_CN = json:string_of("continent.names.zh-CN")
local continentNamesDe = json:string_of("continent.names.de")
local continentNamesEn = json:string_of("continent.names.en")
local continentNamesEs = json:string_of("continent.names.es")
local continentNamesFr = json:string_of("continent.names.fr")
local countryIs_in_european_union = json:bool_of("country.is_in_european_union")
local countryIso_code = json:string_of("country.iso_code")
local countryGeoname_id = json:int_of("country.geoname_id")
local countryNamesFr = json:string_of("country.names.fr")
local countryNamesJa = json:string_of("country.names.ja")
local countryNamesPt_BR = json:string_of("country.names.pt-BR")
local countryNamesRu = json:string_of("country.names.ru")
local countryNamesZh_CN = json:string_of("country.names.zh-CN")
local countryNamesDe = json:string_of("country.names.de")
local countryNamesEn = json:string_of("country.names.en")
local countryNamesEs = json:string_of("country.names.es")
local maxmindQueries_remaining = json:int_of("maxmind.queries_remaining")
local registered_countryIs_in_european_union = json:bool_of("registered_country.is_in_european_union")
local registered_countryIso_code = json:string_of("registered_country.iso_code")
local registered_countryGeoname_id = json:int_of("registered_country.geoname_id")
local registered_countryNamesEs = json:string_of("registered_country.names.es")
local registered_countryNamesFr = json:string_of("registered_country.names.fr")
local registered_countryNamesJa = json:string_of("registered_country.names.ja")
local registered_countryNamesPt_BR = json:string_of("registered_country.names.pt-BR")
local registered_countryNamesRu = json:string_of("registered_country.names.ru")
local registered_countryNamesZh_CN = json:string_of("registered_country.names.zh-CN")
local registered_countryNamesDe = json:string_of("registered_country.names.de")
local registered_countryNamesEn = json:string_of("registered_country.names.en")
local traitsIp_address = json:string_of("traits.ip_address")