AutoIt Requires Chilkat v11.0.0+
AutoIt
Look up an IP address with ipstack over HTTPS
See more Geolocation Examples
Set your ipstack access key and request one public IP address over HTTPS. HTTP status errors and the API’s success=false error object are checked separately. Account quotas and optional modules depend on the subscription. The example displays core geolocation fields and handles null values.
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"
Local $sAccessKey = "YOUR_ACCESS_KEY"
$oUrl = ObjCreate("Chilkat.StringBuilder")
$oUrl.Append("https://api.ipstack.com/")
$oUrl.Append($oHttp.UrlEncode($sIpAddress))
$oParameters = ObjCreate("Chilkat.JsonObject")
$oParameters.UpdateString("access_key",$sAccessKey)
$oResponse = ObjCreate("Chilkat.HttpResponse")
$bSuccess = $oHttp.HttpParams("GET",$oUrl.GetAsString(),$oParameters,$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("error.type") = 1) Then
ConsoleWrite($oJson.StringOf("error.type") & @CRLF)
EndIf
If ($oJson.JsonTypeOf("error.info") = 1) Then
ConsoleWrite($oJson.StringOf("error.info") & @CRLF)
EndIf
EndIf
Exit
EndIf
If ($bParsed <> True) Then
ConsoleWrite("Expected a JSON object in the lookup response." & @CRLF)
Exit
EndIf
If ((($oJson.JsonTypeOf("success") = 5) And ($oJson.BoolOf("success") <> True)) Or ($oJson.JsonTypeOf("error") = 3)) Then
ConsoleWrite("The service could not complete the lookup." & @CRLF)
If ($oJson.JsonTypeOf("error.type") = 1) Then
ConsoleWrite($oJson.StringOf("error.type") & @CRLF)
EndIf
If ($oJson.JsonTypeOf("error.info") = 1) Then
ConsoleWrite($oJson.StringOf("error.info") & @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_name") = 1) And ($oJson.StringOfEquals("region_name","",True) <> True)) Then
ConsoleWrite("Region: " & $oJson.StringOf("region_name") & @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("zip") = 1) And ($oJson.StringOfEquals("zip","",True) <> True)) Then
ConsoleWrite("Postal code: " & $oJson.StringOf("zip") & @CRLF)
Else
ConsoleWrite("Postal code: 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