PHP – USPS Address Verification

The following php example helps you get the address verfication using the USPS web service. 
You only need to set the Lincence Number, User ID and Password. 

Code Example:

<?php
$xml_string	 = "";
$xml_string   = "<?xml version=\"1.0\" ?>";
$xml_string  .= "<AccessRequest xml:lang=\"en-US\">";
$xml_string  .=          "<AccessLicenseNumber>LICENCE_NUMBER</AccessLicenseNumber>";
$xml_string  .=          "<UserId>USER_ID</UserId>";
$xml_string  .=          "<Password>PASSWORD</Password>";
$xml_string  .= "</AccessRequest>";
$xml_string  .= "<?xml version=\"1.0\" ?>";
$xml_string  .= "<AddressValidationRequest xml:lang=\"en-US\">";
$xml_string  .=		"<Request>";
$xml_string  .=			"<TransactionReference>";
$xml_string  .=				"<CustomerContext>Address Validation Request</CustomerContext>";
$xml_string  .=				"<XpciVersion>1.0001</XpciVersion>";
$xml_string  .=			"</TransactionReference>";
$xml_string  .=			"<RequestAction>AV</RequestAction>";
$xml_string  .=		"</Request>";
$xml_string  .=		"<Address>";
$xml_string  .=			"<CountryCode>US</CountryCode>";
$xml_string  .=			"<City>Escondido</City>";
$xml_string  .=			"<State>CA</State>";
$xml_string  .=			"<PostalCode>92082</PostalCode>";
$xml_string  .=		"</Address>";
$xml_string  .= "</AddressValidationRequest>";

$ch = curl_init(); 	
curl_setopt ($ch, CURLOPT_URL,"https://onlinetools.ups.com/ups.app/xml/AV");  /// the url to post to without the ?query+string
curl_setopt ($ch, CURLOPT_HEADER, 0); /// header control
curl_setopt($ch, CURLOPT_POST, 1); // tell curl to do a POST, not a GET
curl_setopt($ch, CURLOPT_VERBOSE, 1); // tell curl to do a verbose
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // tell curl to do a verbose
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_string); /// the query string goes here as set in the variable above
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); /// allows it to set the response in a variable - $xyz
curl_setopt ($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml;')); /// allows it to set the response in a variable - $xyz
$myresult = curl_exec ($ch); /// execute the curl session and store the response in the variable $xyz
curl_close ($ch); /// close the curl session
echo htmlentities($myresult);
?>

Post to Twitter Post to Digg Post to Facebook Post to Google Buzz Send Gmail