Jump to content

Autoit address validation


Go to solution Solved by Danp2,

Recommended Posts

I need to validate addresses used / captured in an Autoit app. These addresses are not validated, prone to misstakes and errors.

I see that Chilkat has a good example script for their API, however, the  $300 fee is too steep for me.

I see that Loqate has an API for $100, much better price. It runs at $.044 per lookup. I use it untill the $100.00 is used up

I am looking for an example script for Loqate or any other API that is around the $100 range or less.

 

Thanks

Edited by AutoitMike
Link to comment
Share on other sites

USPS offers a free API that I've used in the past to perform address verification (registration / approval is required). This is the function that I previously wrote --

#include <WinHttp.au3>
#include <String.au3>

; #CONSTANTS# ===============================================================================
Global Const $resultValid = 1
Global Const $resultInvalid = 2
Global Const $resultMultiple = 3
Global Const $resultVacant = 4
;============================================================================================

; #FUNCTION# ;===============================================================================
; Name...........: _ValidateAddressUSPSAPI
; Description ...:
; Syntax.........: _ValidateAddress($cAddr1, $cAddr2, $cCity, $cState, $cZip = '')
; Parameters ....: $cAddr1 - String representing the street portion of address
;                  $cAddr2 - String representing additional street portion of address
;                  $cCity - String representing city portion of address
;                  $cState - String representing state portion of address
;                  $cZip - String representing zipcode portion of address
; Return values .: Success - Returns result code representing validity of address
;                  Failure - Returns 0 and sets @error:
;                  |1 - _WinHttpConnect failure.
;                  |2 - _WinHttpSimpleSendRequest failure.
;                  |3 - USPS API error
; Author ........: DanP
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......:
;============================================================================================
Func _ValidateAddressUSPSAPI($cAddr1, $cAddr2, $cCity, $cState, $cZip = '')

Const $userID = "XXXXXXXXXXXX" ; Change this to your assigned user name from USPS

Const $url0 = "production.shippingapis.com"
Const $url1 = '/ShippingAPI.dll?API=ZipCodeLookup&XML=<ZipCodeLookupRequest USERID="'
Const $url2 = '"><Address ID="0"><Address1></Address1><Address2>'
Const $url3 = "</Address2><City>"
Const $url4 = "</City><State>"
Const $url5 = "</State></Address></ZipCodeLookupRequest>"

Const $mrkZip4_1 = '<Zip4>'
Const $mrkZip4_2 = '</Zip4>'
Const $mrkError_1 = '<error>'
Const $mrkError_2 = '</error>'
Const $mrkNumber_1 = '<number>'
Const $mrkNumber_2 = '</number>'
Const $mrkReturn_1 = '<ReturnText>'
Const $mrkReturn_2 = '</ReturnText>'

Const $codeAuthFailure = '80040b1a'

Local $nResult, $aZip4, $aError

; Initialize and get session handle
Local $hOpen = _WinHttpOpen()

; Get connection handle
Local $hConnect = _WinHttpConnect($hOpen, $url0)

If @error Then
    Return SetError(1, 0, 0)
EndIf

Local $fullUrl =  $url1 & __WinHttpURLEncode($userID) & $url2 & __WinHttpURLEncode($cAddr1) & __WinHttpURLEncode($cAddr2) & $url3 & __WinHttpURLEncode($cCity) & $url4 & __WinHttpURLEncode($cState) & $url5

 ConsoleWrite($fullUrl & @CRLF)

; Make a request
Global $hRequest = _WinHttpSimpleSendRequest($hConnect, Default, $fullUrl)

If @error Then
    _WinHttpCloseHandle($hRequest)
    Return SetError(2, 0, 0)
EndIf

If $hRequest Then
    Local $cReply

    $cReply = _WinHttpSimpleReadData($hRequest)

 ConsoleWrite($cReply & @CRLF)

    If StringInStr($cReply, $mrkError_1) Then
        $aError = _StringBetween($cReply, $mrkNumber_1, $mrkNumber_2)

        If @error = 0 And $aError[0] = $codeAuthFailure Then
            ; Authorization failure
            Return SetError(3, 0, 0)
        Else
            $nResult = $resultInvalid
        EndIf
    Else
        $aZip4 = _StringBetween($cReply, $mrkZip4_1, $mrkZip4_2, -1)

        If @error Or IsArray($aZip4) = 0 Or StringLen($aZip4[0]) = 0 Then
            ConsoleWrite('@error = ' & @error & @CRLF)
            ConsoleWrite('zip4 error' & @crlf)
            ConsoleWrite('IsArray($aZip4) = ' & IsArray($aZip4) & @CRLF)

            $nResult = $resultInvalid
        Else
            If StringInStr($cReply, $mrkReturn_1) Then
                $nResult = $resultMultiple
            Else
                $nResult = $resultValid
            EndIf
        EndIf
    EndIf
EndIf

; ConsoleWrite('Result = ' & $nResult & @CRLF)

; Close handles
_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)

Return $nResult
EndFunc

 

Link to comment
Share on other sites

An alternative to their API is to automate the USPS website query --

#FUNCTION# ;===============================================================================
; Name...........: _ValidateAddressUSPS
; Description ...:
; Syntax.........: _ValidateAddress($cAddr1, $cAddr2, $cCity, $cState, $cZip = '')
; Parameters ....: $cAddr1 - String representing the street portion of address
;                  $cAddr2 - String representing additional street portion of address
;                  $cCity - String representing city portion of address
;                  $cState - String representing state portion of address
;                  $cZip - String representing zipcode portion of address
; Return values .: Success - Returns result code representing validity of address
;                  Failure - Returns 0 and sets @error:
;                  |1 - _WinHttpConnect failure.
;                  |2 - _WinHttpSimpleSendRequest failure.
; Author ........: DanP
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......:
;============================================================================================
Func _ValidateAddressUSPS($cAddr1, $cAddr2, $cCity, $cState, $cZip = '')

Const $url0 = "tools.usps.com"
Const $url1 = "/go/ZipLookupResultsAction!input.action?resultMode=0&companyName=&address1="
Const $url2 = "&address2="
Const $url3 = "&city="
Const $url4 = "&state="
Const $url5 = "&urbanCode=&postalCode=&zip="

Const $responseValid = "Here's the full address"
Const $responseMultiple = "Several addresses matched"
Const $responseInvalid = "Unfortunately, this address wasn't found"
Const $responseInvalid2 = "not recognized by the US Postal Service"

Local $nResult

; Initialize and get session handle
Local $hOpen = _WinHttpOpen()

; Get connection handle
Local $hConnect = _WinHttpConnect($hOpen, $url0)

If @error Then
    Return SetError(1, 0, 0)
EndIf

Local $fullUrl =  $url1 & __WinHttpURLEncode($cAddr1) & $url2 & __WinHttpURLEncode($cAddr2) & $url3 & __WinHttpURLEncode($cCity) & $url4 & __WinHttpURLEncode($cState) & $url5 & __WinHttpURLEncode($cZip)

ConsoleWrite($fullUrl)

; Make a request
Global $hRequest = _WinHttpSimpleSendRequest($hConnect, Default, $fullUrl)

If @error Then
    _WinHttpCloseHandle($hRequest)
    Return SetError(2, 0, 0)
EndIf

If $hRequest Then
    Local $cReply

    $cReply = _WinHttpSimpleReadData($hRequest)

    Select
        Case StringInStr($cReply, $responseInvalid) Or StringInStr($cReply, $responseInvalid2)
            $nResult = $resultInvalid

        Case StringInStr($cReply, $responseValid)
            $nResult = $resultValid

        Case StringInStr($cReply, $responseMultiple)
            $nResult = $resultMultiple
    EndSelect
EndIf

; Close handles
_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)

Return $nResult
EndFunc

 

Link to comment
Share on other sites

You could also look into Melissa Data (now know as simply Melissa), which has APIs for this stuff. I know this will no longer work because of changes to their website, but maybe it will still be of some use --

Func _ValidateAddressMelissaData($cData)

Const $url0 = "www.melissadata.com"
Const $url1 = "/lookups/AddressCheck.asp?InData="

Const $responseValid = "AS01"
Const $responseMultiple = "AS02"
Const $responseZipChanged = "AC01"
Const $responseVacant = "AS16"
Const $responseNoMail = "AS17"
Const $responseAptMissing = "AE09"
Const $responseNumberInvalid = "AE10"
Const $responseNumberMissing = "AE11"
Const $responseBoxInvalid = "AE12"
Const $responseBoxMissing = "AE13"
Const $responsePMBMIssing = "AE14"

Local $nResult, $cReply

; Initialize and get session handle
Local $hOpen = _WinHttpOpen()

; Get connection handle
Local $hConnect = _WinHttpConnect($hOpen, $url0)

If @error Then
    Return SetError(1, 0, 0)
EndIf

Local $fullUrl =  $url1 & __WinHttpURLEncode($cData)

; Make a request
Global $hRequest = _WinHttpSimpleSendRequest($hConnect, Default, $fullUrl)

If @error Then
    _WinHttpCloseHandle($hRequest)
    Return SetError(2, 0, 0)
EndIf

If $hRequest Then
    $cReply = _WinHttpSimpleReadData($hRequest)

;   ConsoleWrite($cReply)

    Select
        Case StringInStr($cReply, $responseNumberInvalid) Or StringInStr($cReply, $responseNumberMissing) Or StringInStr($cReply, $responseBoxInvalid) Or StringInStr($cReply, $responseBoxMissing) Or StringInStr($cReply, $responseNumberMissing)
            $nResult = $resultInvalid

        Case StringInStr($cReply, $responseMultiple) Or StringInStr($cReply, $responseAptMissing)
            $nResult = $resultMultiple

        Case StringInStr($cReply, $responseVacant) Or StringInStr($cReply, $responseNoMail)
            $nResult = $resultVacant

        Case StringInStr($cReply, $responseValid)
            $nResult = $resultValid

    EndSelect
EndIf

; Close handles
_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)

Return $nResult
EndFunc

 

Link to comment
Share on other sites

Thanks for the quick reply and the code.

Does your second example also require registration and approval from the PO?

If so how do I go about doing this.

Also, 

It looks like I need WinHttp.au3 for all examples (2000 lines of code OMG)

 

Link to comment
Share on other sites

A number of years ago I used to do address verification by automating the USPS website too, but they really frown on that. And they imposed a limit to how many addresses you can verify through the website within a certain time. Then you have to wait a little while before you can do some more. It got aggravating enough (which is the reason they do that) that I went ahead and registered for a free account with them and started using the API. That's been working great for years now.

Link to comment
Share on other sites

Dan,

I used the following on the second example and I get a '0' return value:

$C1=_ValidateAddressUSPS('3225 Courthouse dr','','Union City','Ca','94587')
msgbox(0,'',$C1)

But I just now realized that you said it wont work.

However, I dont understand the part $cZip = '  '

Is the command line wrong?

Edited by AutoitMike
Link to comment
Share on other sites

Dan,

After studying the code I now see that I need to look at $cReply

I assumed that the function returned the valid address.

I will modify my copy to return $cReplly

Thanks again for the help

Edited by AutoitMike
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...