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.
Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
String sIpAddress
Handle hoHttp
Handle hoUrl
Variant vResponse
Handle hoResponse
Handle hoJson
Boolean iParsed
String sTemp1
Integer iTemp1
Integer iTemp2
Boolean bTemp1
Move False To iSuccess
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Use a public IP address. Provider results can change as their databases are updated.
Move "8.8.8.8" To sIpAddress
Get Create (RefClass(cComChilkatHttp)) To hoHttp
If (Not(IsComObjectCreated(hoHttp))) Begin
Send CreateComObject of hoHttp
End
Set ComRequireSslCertVerify Of hoHttp To True
Set ComConnectTimeout Of hoHttp To 10
Set ComReadTimeout Of hoHttp To 30
Set ComAccept Of hoHttp To "application/json"
Set ComBasicAuth Of hoHttp To True
Set ComLogin Of hoHttp To "MAXMIND_ACCOUNT_ID"
Set ComPassword Of hoHttp To "MAXMIND_LICENSE_KEY"
Get Create (RefClass(cComChilkatStringBuilder)) To hoUrl
If (Not(IsComObjectCreated(hoUrl))) Begin
Send CreateComObject of hoUrl
End
Get ComAppend Of hoUrl "https://geoip.maxmind.com/geoip/v2.1/country/" To iSuccess
Get ComUrlEncode Of hoHttp sIpAddress To sTemp1
Get ComAppend Of hoUrl sTemp1 To iSuccess
Get Create (RefClass(cComChilkatHttpResponse)) To hoResponse
If (Not(IsComObjectCreated(hoResponse))) Begin
Send CreateComObject of hoResponse
End
Get ComGetAsString Of hoUrl To sTemp1
Get pvComObject of hoResponse to vResponse
Get ComHttpNoBody Of hoHttp "GET" sTemp1 vResponse To iSuccess
Get ComStatusCode Of hoResponse To iTemp1
If (iTemp1 = 0) Begin
// Request diagnostics can contain an API key. Report transport failure without logging credentials.
Showln "No HTTP response. Check connectivity, DNS, and TLS configuration."
Procedure_Return
End
Get Create (RefClass(cComChilkatJsonObject)) To hoJson
If (Not(IsComObjectCreated(hoJson))) Begin
Send CreateComObject of hoJson
End
Get ComBodyStr Of hoResponse To sTemp1
Get ComLoad Of hoJson sTemp1 To iParsed
Get ComStatusCode Of hoResponse To iTemp1
Get ComStatusCode Of hoResponse To iTemp2
If ((iSuccess <> True) Or (iTemp1 < 200) Or (iTemp2 >= 300)) Begin
Get ComStatusCode Of hoResponse To iTemp1
Showln "Lookup failed. HTTP " iTemp1
If (iParsed = True) Begin
Get ComJsonTypeOf Of hoJson "code" To iTemp1
If (iTemp1 = 1) Begin
Get ComStringOf Of hoJson "code" To sTemp1
Showln sTemp1
End
Get ComJsonTypeOf Of hoJson "error" To iTemp1
If (iTemp1 = 1) Begin
Get ComStringOf Of hoJson "error" To sTemp1
Showln sTemp1
End
End
Procedure_Return
End
If (iParsed <> True) Begin
Showln "Expected a JSON object in the lookup response."
Procedure_Return
End
Get ComJsonTypeOf Of hoJson "traits.ip_address" To iTemp1
Get ComStringOfEquals Of hoJson "traits.ip_address" "" True To bTemp1
If ((iTemp1 <> 1) Or (bTemp1 = True)) Begin
Showln "The response did not contain the expected IP address."
Procedure_Return
End
Get ComStringOf Of hoJson "traits.ip_address" To sTemp1
Showln "IP address: " sTemp1
Get ComJsonTypeOf Of hoJson "country.names.en" To iTemp1
Get ComStringOfEquals Of hoJson "country.names.en" "" True To bTemp1
If ((iTemp1 = 1) And (bTemp1 <> True)) Begin
Get ComStringOf Of hoJson "country.names.en" To sTemp1
Showln "Country: " sTemp1
End
Else Begin
Showln "Country: unavailable"
End
Get ComJsonTypeOf Of hoJson "country.iso_code" To iTemp1
Get ComStringOfEquals Of hoJson "country.iso_code" "" True To bTemp1
If ((iTemp1 = 1) And (bTemp1 <> True)) Begin
Get ComStringOf Of hoJson "country.iso_code" To sTemp1
Showln "Country code: " sTemp1
End
Else Begin
Showln "Country code: unavailable"
End
Get ComJsonTypeOf Of hoJson "continent.names.en" To iTemp1
Get ComStringOfEquals Of hoJson "continent.names.en" "" True To bTemp1
If ((iTemp1 = 1) And (bTemp1 <> True)) Begin
Get ComStringOf Of hoJson "continent.names.en" To sTemp1
Showln "Continent: " sTemp1
End
Else Begin
Showln "Continent: unavailable"
End
Get ComJsonTypeOf Of hoJson "registered_country.iso_code" To iTemp1
Get ComStringOfEquals Of hoJson "registered_country.iso_code" "" True To bTemp1
If ((iTemp1 = 1) And (bTemp1 <> True)) Begin
Get ComStringOf Of hoJson "registered_country.iso_code" To sTemp1
Showln "Registered country code: " sTemp1
End
Else Begin
Showln "Registered country code: unavailable"
End
End_Procedure