Friday, June 3, 2011

UPS address verification system(XAV)

Code to check about address validation for the UPS shipping.

Below code can be use to check whether customer shipping address is valid according to the UPS/USPS database or not.

//STREET LEVEL ADDRESS VARIFICATION REQUEST

$xmlRequest1='<?xml version="1.0"?>
<AccessRequest xml:lang="en-US">
<AccessLicenseNumber>ACCESS LICENCE NUMBER</AccessLicenseNumber>
<UserId>UPS USERNAME</UserId>
<Password>UPS PASSWORD</Password>
</AccessRequest>
<?xml version="1.0"?>
<AddressValidationRequest xml:lang="en-US">
<Request>
<TransactionReference>
<CustomerContext>Your Test Case Summary Description</CustomerContext>
<XpciVersion>1.0</XpciVersion>
</TransactionReference>
<RequestAction>XAV</RequestAction>
<RequestOption>3</RequestOption>
</Request>

<AddressKeyFormat>
<AddressLine>AIRWAY ROAD SUITE 7</AddressLine>
<PoliticalDivision2>SAN DIEGO</PoliticalDivision2>
<PoliticalDivision1>CA</PoliticalDivision1>
<PostcodePrimaryLow>92154</PostcodePrimaryLow>
<CountryCode>US</CountryCode>
</AddressKeyFormat>
</AddressValidationRequest>';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://wwwcie.ups.com/ups.app/xml/XAV");
// uncomment the next line if you get curl error 60: error setting certificate verify locations
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
// uncommenting the next line is most likely not necessary in case of error 60
// curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlRequest1);
curl_setopt($ch, CURLOPT_TIMEOUT, 3600);

//if ($this->logfile) {
// error_log("UPS REQUEST: " . $xmlRequest . "\n", 3, $this->logfile);
//}
echo $xmlResponse = curl_exec ($ch); // SHIP CONFIRMATION RESPONSE
$xml = $xmlResponse;

preg_match_all( "/\<AddressValidationResponse\>(.*?)\<\/AddressValidationResponse\>/s",
$xml, $upsRes );

foreach( $upsRes[1] as $res )
{
preg_match_all( "/\<ResponseStatusCode\>(.*?)\<\/ResponseStatusCode\>/",
$res, $response ); // XAV CODE


preg_match_all( "/\<ResponseStatusDescription\>(.*?)\<\/ResponseStatusDescription\>/",
$res, $responseMsg ); // XAV MESSAGE

preg_match_all( "/\<AddressLine\>(.*?)\<\/AddressLine\>/",
$res, $address ); // Possible Address Line

preg_match_all( "/\<Region\>(.*?)\<\/Region\>/",
$res, $Region ); // Possible region

preg_match_all( "/\<PoliticalDivision2\>(.*?)\<\/PoliticalDivision2\>/",
$res, $city );

preg_match_all( "/\<PoliticalDivision1\>(.*?)\<\/PoliticalDivision1\>/",
$res, $state );

preg_match_all( "/\<PostcodePrimaryLow\>(.*?)\<\/PostcodePrimaryLow\>/",
$res, $postCode );

preg_match_all( "/\<PostcodeExtendedLow\>(.*?)\<\/PostcodeExtendedLow\>/",
$res, $extenCode );

}
for($i=0;$i<count($address[1]);$i++)
{
echo $address[1][$i];
echo "<br>";
echo $Region[1][$i];
echo "<br>";
echo $city[1][$i];
echo "<br>";
echo $state[1][$i];
echo "<br>";
echo $postCode[1][$i];
echo "<br>";
echo $extenCode[1][$i];
echo "<br>";
echo "<hr>";
}

No comments:

Post a Comment