Sample code for 30+ languages & platforms
Xbase++

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oHttp
LOCAL cJsonStr
LOCAL oJson
LOCAL cIp
LOCAL cV_type
LOCAL cContinent_code
LOCAL cContinent_name
LOCAL cCountry_code
LOCAL cCountry_name
LOCAL cRegion_code
LOCAL cRegion_name
LOCAL cCity
LOCAL cZip
LOCAL cLatitude
LOCAL cLongitude
LOCAL cLocationGeoname_id
LOCAL cLocationCapital
LOCAL cLocationCountry_flag
LOCAL cLocationCountry_flag_emoji
LOCAL cLocationCountry_flag_emoji_unicode
LOCAL cLocationCalling_code
LOCAL nLocationIs_eu
LOCAL i
LOCAL nCount_i
LOCAL cCode
LOCAL cName
LOCAL cNative

nSuccess := 0

//  This example requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

oHttp := CreateObject("Chilkat.Http")

//  Lookup an IPv4 address: 149.250.207.170  (this was a randomly chosen address)
cJsonStr := oHttp:QuickGetStr("http://api.ipstack.com/149.250.207.170?access_key=YOUR_ACCESS_KEY")
IF (oHttp:LastMethodSuccess == 0)
    ? oHttp:LastErrorText
    oHttp:destroy()
    RETURN
ENDIF

oJson := CreateObject("Chilkat.JsonObject")
oJson:EmitCompact := 0
nSuccess := oJson:Load(cJsonStr)

? oJson: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
//    }
//  }

cIp := oJson:StringOf("ip")
cV_type := oJson:StringOf("type")
cContinent_code := oJson:StringOf("continent_code")
cContinent_name := oJson:StringOf("continent_name")
cCountry_code := oJson:StringOf("country_code")
cCountry_name := oJson:StringOf("country_name")
cRegion_code := oJson:StringOf("region_code")
cRegion_name := oJson:StringOf("region_name")
cCity := oJson:StringOf("city")
cZip := oJson:StringOf("zip")
cLatitude := oJson:StringOf("latitude")
cLongitude := oJson:StringOf("longitude")
cLocationGeoname_id := oJson:StringOf("location.geoname_id")
cLocationCapital := oJson:StringOf("location.capital")
cLocationCountry_flag := oJson:StringOf("location.country_flag")
cLocationCountry_flag_emoji := oJson:StringOf("location.country_flag_emoji")
cLocationCountry_flag_emoji_unicode := oJson:StringOf("location.country_flag_emoji_unicode")
cLocationCalling_code := oJson:StringOf("location.calling_code")
nLocationIs_eu := oJson:BoolOf("location.is_eu")
i := 0
nCount_i := oJson:SizeOfArray("location.languages")
DO WHILE i < nCount_i
    oJson:I := i
    cCode := oJson:StringOf("location.languages[i].code")
    cName := oJson:StringOf("location.languages[i].name")
    cNative := oJson:StringOf("location.languages[i].native")
    i := i + 1
ENDDO

oHttp:destroy()
oJson:destroy()