AutoIt Requires Chilkat v11.0.0+
AutoIt
Look up an IP address with ipapi.co
See more Geolocation Examples
Request the HTTPS JSON endpoint for a public IPv4 or IPv6 address. The sample uses the keyless service for evaluation. Use your subscription credentials and limits for production traffic.
Check both the HTTP status and the JSON error flag. An invalid or reserved address can return HTTP 200 with an error object. Unknown location fields are returned as null. The example keeps those values unavailable instead of displaying zero coordinates.
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 AutoIt Downloads
Local $bSuccess = False
; Assumes Chilkat is unlocked. See Global Unlock Sample.
; Use a public IP address. Provider results can change as their databases are updated.
Local $sIpAddress = "8.8.8.8"
$oHttp = ObjCreate("Chilkat.Http")
$oHttp.RequireSslCertVerify = True
$oHttp.ConnectTimeout = 10
$oHttp.ReadTimeout = 30
$oHttp.Accept = "application/json"
$oUrl = ObjCreate("Chilkat.StringBuilder")
$oUrl.Append("https://ipapi.co/")
$oUrl.Append($oHttp.UrlEncode($sIpAddress))
$oUrl.Append("/json/")
$oResponse = ObjCreate("Chilkat.HttpResponse")
$bSuccess = $oHttp.HttpNoBody("GET",$oUrl.GetAsString(),$oResponse)
If ($oResponse.StatusCode = 0) Then
; Request diagnostics can contain an API key. Report transport failure without logging credentials.
ConsoleWrite("No HTTP response. Check connectivity, DNS, and TLS configuration." & @CRLF)
Exit
EndIf
$oJson = ObjCreate("Chilkat.JsonObject")
Local $bParsed = $oJson.Load($oResponse.BodyStr)
If (($bSuccess <> True) Or ($oResponse.StatusCode < 200) Or ($oResponse.StatusCode >= 300)) Then
ConsoleWrite("Lookup failed. HTTP " & $oResponse.StatusCode & @CRLF)
If ($bParsed = True) Then
If ($oJson.JsonTypeOf("reason") = 1) Then
ConsoleWrite($oJson.StringOf("reason") & @CRLF)
EndIf
If ($oJson.JsonTypeOf("message") = 1) Then
ConsoleWrite($oJson.StringOf("message") & @CRLF)
EndIf
EndIf
Exit
EndIf
If ($bParsed <> True) Then
ConsoleWrite("Expected a JSON object in the lookup response." & @CRLF)
Exit
EndIf
If (($oJson.JsonTypeOf("error") = 5) And ($oJson.BoolOf("error") = True)) Then
ConsoleWrite("The service could not complete the lookup." & @CRLF)
If ($oJson.JsonTypeOf("reason") = 1) Then
ConsoleWrite($oJson.StringOf("reason") & @CRLF)
EndIf
If ($oJson.JsonTypeOf("message") = 1) Then
ConsoleWrite($oJson.StringOf("message") & @CRLF)
EndIf
Exit
EndIf
If (($oJson.JsonTypeOf("ip") <> 1) Or ($oJson.StringOfEquals("ip","",True) = True)) Then
ConsoleWrite("The response did not contain the expected IP address." & @CRLF)
Exit
EndIf
ConsoleWrite("IP address: " & $oJson.StringOf("ip") & @CRLF)
If (($oJson.JsonTypeOf("country_name") = 1) And ($oJson.StringOfEquals("country_name","",True) <> True)) Then
ConsoleWrite("Country: " & $oJson.StringOf("country_name") & @CRLF)
Else
ConsoleWrite("Country: unavailable" & @CRLF)
EndIf
If (($oJson.JsonTypeOf("country_code") = 1) And ($oJson.StringOfEquals("country_code","",True) <> True)) Then
ConsoleWrite("Country code: " & $oJson.StringOf("country_code") & @CRLF)
Else
ConsoleWrite("Country code: unavailable" & @CRLF)
EndIf
If (($oJson.JsonTypeOf("region") = 1) And ($oJson.StringOfEquals("region","",True) <> True)) Then
ConsoleWrite("Region: " & $oJson.StringOf("region") & @CRLF)
Else
ConsoleWrite("Region: unavailable" & @CRLF)
EndIf
If (($oJson.JsonTypeOf("city") = 1) And ($oJson.StringOfEquals("city","",True) <> True)) Then
ConsoleWrite("City: " & $oJson.StringOf("city") & @CRLF)
Else
ConsoleWrite("City: unavailable" & @CRLF)
EndIf
If (($oJson.JsonTypeOf("timezone") = 1) And ($oJson.StringOfEquals("timezone","",True) <> True)) Then
ConsoleWrite("Time zone: " & $oJson.StringOf("timezone") & @CRLF)
Else
ConsoleWrite("Time zone: unavailable" & @CRLF)
EndIf
; Missing coordinates stay unavailable. A numeric zero is a valid coordinate.
If (($oJson.JsonTypeOf("latitude") = 2) And ($oJson.JsonTypeOf("longitude") = 2)) Then
ConsoleWrite("Coordinates: " & $oJson.StringOf("latitude") & ", " & $oJson.StringOf("longitude") & @CRLF)
Else
ConsoleWrite("Coordinates: unavailable" & @CRLF)
EndIf