Sample code for 30+ languages & platforms
Visual FoxPro Requires Chilkat v11.0.0+

Look up an IP country with MaxMind GeoIP

See more Geolocation Examples

Call the GeoIP Country web service using HTTP Basic authentication. Set the username to your numeric MaxMind account ID and the password to your license key. The account needs access to the web service, such as a trial or service credit. Both IPv4 and IPv6 are supported.

country describes the geolocated country. registered_country describes where the network is registered. The Country service returns country-level data. City-level lookups use the separately entitled /geoip/v2.1/city/ service. A GeoLite web-service account uses geolite.info as its host.

IP geolocation estimates a network location. VPNs, mobile gateways, and anycast routing can affect the result. City and coordinate fields can be missing or approximate. The sample address 8.8.8.8 is a public DNS address used to demonstrate the request.

Service documentation

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL lcIpAddress
LOCAL loHttp
LOCAL loUrl
LOCAL loResponse
LOCAL loJson
LOCAL lnParsed

lnSuccess = 0

*  Assumes Chilkat is unlocked. See Global Unlock Sample.
*  Use a public IP address. Provider results can change as their databases are updated.
lcIpAddress = "8.8.8.8"
loHttp = CreateObject('Chilkat.Http')
loHttp.RequireSslCertVerify = 1
loHttp.ConnectTimeout = 10
loHttp.ReadTimeout = 30
loHttp.Accept = "application/json"
loHttp.BasicAuth = 1
loHttp.Login = "MAXMIND_ACCOUNT_ID"
loHttp.Password = "MAXMIND_LICENSE_KEY"
loUrl = CreateObject('Chilkat.StringBuilder')
loUrl.Append("https://geoip.maxmind.com/geoip/v2.1/country/")
loUrl.Append(loHttp.UrlEncode(lcIpAddress))
loResponse = CreateObject('Chilkat.HttpResponse')
lnSuccess = loHttp.HttpNoBody("GET",loUrl.GetAsString(),loResponse)
IF (loResponse.StatusCode = 0) THEN
    *  Request diagnostics can contain an API key. Report transport failure without logging credentials.
    ? "No HTTP response. Check connectivity, DNS, and TLS configuration."
    RELEASE loHttp
    RELEASE loUrl
    RELEASE loResponse
    CANCEL
ENDIF

loJson = CreateObject('Chilkat.JsonObject')
lnParsed = loJson.Load(loResponse.BodyStr)
IF ((lnSuccess <> 1) OR (loResponse.StatusCode < 200) OR (loResponse.StatusCode >= 300)) THEN
    ? "Lookup failed. HTTP " + STR(loResponse.StatusCode)
    IF (lnParsed = 1) THEN
        IF (loJson.JsonTypeOf("code") = 1) THEN
            ? loJson.StringOf("code")
        ENDIF

        IF (loJson.JsonTypeOf("error") = 1) THEN
            ? loJson.StringOf("error")
        ENDIF

    ENDIF

    RELEASE loHttp
    RELEASE loUrl
    RELEASE loResponse
    RELEASE loJson
    CANCEL
ENDIF

IF (lnParsed <> 1) THEN
    ? "Expected a JSON object in the lookup response."
    RELEASE loHttp
    RELEASE loUrl
    RELEASE loResponse
    RELEASE loJson
    CANCEL
ENDIF

IF ((loJson.JsonTypeOf("traits.ip_address") <> 1) OR (loJson.StringOfEquals("traits.ip_address","",1) = 1)) THEN
    ? "The response did not contain the expected IP address."
    RELEASE loHttp
    RELEASE loUrl
    RELEASE loResponse
    RELEASE loJson
    CANCEL
ENDIF

? "IP address: " + loJson.StringOf("traits.ip_address")
IF ((loJson.JsonTypeOf("country.names.en") = 1) AND (loJson.StringOfEquals("country.names.en","",1) <> 1)) THEN
    ? "Country: " + loJson.StringOf("country.names.en")
ELSE
    ? "Country: unavailable"
ENDIF

IF ((loJson.JsonTypeOf("country.iso_code") = 1) AND (loJson.StringOfEquals("country.iso_code","",1) <> 1)) THEN
    ? "Country code: " + loJson.StringOf("country.iso_code")
ELSE
    ? "Country code: unavailable"
ENDIF

IF ((loJson.JsonTypeOf("continent.names.en") = 1) AND (loJson.StringOfEquals("continent.names.en","",1) <> 1)) THEN
    ? "Continent: " + loJson.StringOf("continent.names.en")
ELSE
    ? "Continent: unavailable"
ENDIF

IF ((loJson.JsonTypeOf("registered_country.iso_code") = 1) AND (loJson.StringOfEquals("registered_country.iso_code","",1) <> 1)) THEN
    ? "Registered country code: " + loJson.StringOf("registered_country.iso_code")
ELSE
    ? "Registered country code: unavailable"
ENDIF

RELEASE loHttp
RELEASE loUrl
RELEASE loResponse
RELEASE loJson