Sample code for 30+ languages & platforms
PowerBuilder 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 PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
string ls_IpAddress
oleobject loo_Http
oleobject loo_Url
oleobject loo_Response
oleobject loo_Json
long li_Parsed

li_Success = 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
//  Use a public IP address. Provider results can change as their databases are updated.
ls_IpAddress = "8.8.8.8"
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Http.RequireSslCertVerify = 1
loo_Http.ConnectTimeout = 10
loo_Http.ReadTimeout = 30
loo_Http.Accept = "application/json"
loo_Http.BasicAuth = 1
loo_Http.Login = "MAXMIND_ACCOUNT_ID"
loo_Http.Password = "MAXMIND_LICENSE_KEY"
loo_Url = create oleobject
li_rc = loo_Url.ConnectToNewObject("Chilkat.StringBuilder")

loo_Url.Append("https://geoip.maxmind.com/geoip/v2.1/country/")
loo_Url.Append(loo_Http.UrlEncode(ls_IpAddress))
loo_Response = create oleobject
li_rc = loo_Response.ConnectToNewObject("Chilkat.HttpResponse")

li_Success = loo_Http.HttpNoBody("GET",loo_Url.GetAsString(),loo_Response)
if loo_Response.StatusCode = 0 then
    //  Request diagnostics can contain an API key. Report transport failure without logging credentials.
    Write-Debug "No HTTP response. Check connectivity, DNS, and TLS configuration."
    destroy loo_Http
    destroy loo_Url
    destroy loo_Response
    return
end if

loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

li_Parsed = loo_Json.Load(loo_Response.BodyStr)
if (li_Success <> 1) OR (loo_Response.StatusCode < 200) OR (loo_Response.StatusCode >= 300) then
    Write-Debug "Lookup failed. HTTP " + string(loo_Response.StatusCode)
    if li_Parsed = 1 then
        if loo_Json.JsonTypeOf("code") = 1 then
            Write-Debug loo_Json.StringOf("code")
        end if

        if loo_Json.JsonTypeOf("error") = 1 then
            Write-Debug loo_Json.StringOf("error")
        end if

    end if

    destroy loo_Http
    destroy loo_Url
    destroy loo_Response
    destroy loo_Json
    return
end if

if li_Parsed <> 1 then
    Write-Debug "Expected a JSON object in the lookup response."
    destroy loo_Http
    destroy loo_Url
    destroy loo_Response
    destroy loo_Json
    return
end if

if (loo_Json.JsonTypeOf("traits.ip_address") <> 1) OR (loo_Json.StringOfEquals("traits.ip_address","",1) = 1) then
    Write-Debug "The response did not contain the expected IP address."
    destroy loo_Http
    destroy loo_Url
    destroy loo_Response
    destroy loo_Json
    return
end if

Write-Debug "IP address: " + loo_Json.StringOf("traits.ip_address")
if (loo_Json.JsonTypeOf("country.names.en") = 1) AND (loo_Json.StringOfEquals("country.names.en","",1) <> 1) then
    Write-Debug "Country: " + loo_Json.StringOf("country.names.en")
else
    Write-Debug "Country: unavailable"
end if

if (loo_Json.JsonTypeOf("country.iso_code") = 1) AND (loo_Json.StringOfEquals("country.iso_code","",1) <> 1) then
    Write-Debug "Country code: " + loo_Json.StringOf("country.iso_code")
else
    Write-Debug "Country code: unavailable"
end if

if (loo_Json.JsonTypeOf("continent.names.en") = 1) AND (loo_Json.StringOfEquals("continent.names.en","",1) <> 1) then
    Write-Debug "Continent: " + loo_Json.StringOf("continent.names.en")
else
    Write-Debug "Continent: unavailable"
end if

if (loo_Json.JsonTypeOf("registered_country.iso_code") = 1) AND (loo_Json.StringOfEquals("registered_country.iso_code","",1) <> 1) then
    Write-Debug "Registered country code: " + loo_Json.StringOf("registered_country.iso_code")
else
    Write-Debug "Registered country code: unavailable"
end if



destroy loo_Http
destroy loo_Url
destroy loo_Response
destroy loo_Json