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

Look up an IP address with IP2Location.io

See more Geolocation Examples

Look up a public IPv4 or IPv6 address. The sample runs keyless. To use your account allowance, set apiKey and Chilkat will send it as a Bearer token. IP2Location.io recommends header authentication and also accepts its older key query parameter.

Keyless requests have a daily allowance. Registered plans have their own quotas and field availability. Check the provider’s current limits. An HTTP 401 with error 10000 can indicate an invalid key or an exhausted free allowance. Coordinates can default to the capital city when a city is unknown. The returned time_zone is a UTC offset.

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 lcApiKey
LOCAL loParameters
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"
*  Leave empty for keyless evaluation, or set the API key from your account.
lcApiKey = ""
IF (lcApiKey <> "") THEN
    loHttp.AuthToken = lcApiKey
ENDIF

loParameters = CreateObject('Chilkat.JsonObject')
loParameters.UpdateString("ip",lcIpAddress)
loParameters.UpdateString("format","json")
loResponse = CreateObject('Chilkat.HttpResponse')
lnSuccess = loHttp.HttpParams("GET","https://api.ip2location.io/",loParameters,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 loParameters
    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("error.error_message") = 1) THEN
            ? loJson.StringOf("error.error_message")
        ENDIF

    ENDIF

    RELEASE loHttp
    RELEASE loParameters
    RELEASE loResponse
    RELEASE loJson
    CANCEL
ENDIF

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

IF (loJson.JsonTypeOf("error") = 3) THEN
    ? "The service could not complete the lookup."
    IF (loJson.JsonTypeOf("error.error_message") = 1) THEN
        ? loJson.StringOf("error.error_message")
    ENDIF

    RELEASE loHttp
    RELEASE loParameters
    RELEASE loResponse
    RELEASE loJson
    CANCEL
ENDIF

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

? "IP address: " + loJson.StringOf("ip")
IF ((loJson.JsonTypeOf("country_name") = 1) AND (loJson.StringOfEquals("country_name","",1) <> 1)) THEN
    ? "Country: " + loJson.StringOf("country_name")
ELSE
    ? "Country: unavailable"
ENDIF

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

IF ((loJson.JsonTypeOf("region_name") = 1) AND (loJson.StringOfEquals("region_name","",1) <> 1)) THEN
    ? "Region: " + loJson.StringOf("region_name")
ELSE
    ? "Region: unavailable"
ENDIF

IF ((loJson.JsonTypeOf("city_name") = 1) AND (loJson.StringOfEquals("city_name","",1) <> 1)) THEN
    ? "City: " + loJson.StringOf("city_name")
ELSE
    ? "City: unavailable"
ENDIF

IF ((loJson.JsonTypeOf("time_zone") = 1) AND (loJson.StringOfEquals("time_zone","",1) <> 1)) THEN
    ? "UTC offset: " + loJson.StringOf("time_zone")
ELSE
    ? "UTC offset: unavailable"
ENDIF

*  Missing coordinates stay unavailable. A numeric zero is a valid coordinate.
IF ((loJson.JsonTypeOf("latitude") = 2) AND (loJson.JsonTypeOf("longitude") = 2)) THEN
    ? "Coordinates: " + loJson.StringOf("latitude") + ", " + loJson.StringOf("longitude")
ELSE
    ? "Coordinates: unavailable"
ENDIF

RELEASE loHttp
RELEASE loParameters
RELEASE loResponse
RELEASE loJson