Address Standardization & Verification with USPS web tools and PHP
Posted: October 27th, 2009 | Author: jriggs | Filed under: php | Tags: address, php, standard, usps, web tools, zip code | 8 Comments »Before using this code you will need to sign up for an account with USPS webtools here.
Class, save as ‘usps_address_class.php’
<?php class usps { public $account = 'xxxxxxx'; //you need to register for this public $url = 'http://production.shippingapis.com/ShippingAPI.dll'; public $address1, $address2, $city, $state, $zip; public $ship_address1, $ship_address2, $ship_city, $ship_state, $ship_zip; function toXML() { $xml = ' <AddressValidateRequest USERID="' . $this->account . '"><Address ID="1">'; $xml .= '<Address1>' . $this->address1 . '</Address1>'; $xml .= '<Address2>' . $this->address2 . '</Address2>'; $xml .= '<City>' . $this->city . '</City>'; $xml .= '<State>' . $this->state . '</State>'; $xml .= '<Zip5>' . $this->zip . '</Zip5>'; $xml .= '<Zip4></Zip4></Address>'; if ($this->ship_address2 <> ''){ //shipping address $xml .= '<Address ID="2">'; $xml .= '<Address1>' . $this->ship_address1 . '</Address1>'; $xml .= '<Address2>' . $this->ship_address2 . '</Address2>'; $xml .= '<City>' . $this->ship_city . '</City>'; $xml .= '<State>' . $this->ship_state . '</State>'; $xml .= '<Zip5>' . $this->ship_zip . '</Zip5>'; $xml .= '<Zip4></Zip4></Address>'; } $xml .= '</AddressValidateRequest>'; return $xml; } function submit_request() { $ch = curl_init($this->url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, "API=Verify&XML=" . $this->toXML()); curl_setopt($ch, CURLOPT_TIMEOUT, 60); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false); $result = curl_exec($ch); $error = curl_error($ch); if(empty($error)) { return $result; }else{ die(curl_error($ch)); } } } ?>
And a sample page accessing it:
<?php require("usps_address_class.php"); $uspsRequest = new usps(); //class instantiation $uspsRequest->address1 = 'suite 321'; $uspsRequest->address2 = '1600 Pennsylvania Ave NW'; $uspsRequest->city = 'Washington'; $uspsRequest->state = 'DC'; $uspsRequest->zip = '20500'; /* //optional second address $uspsRequest->ship_address1 = ''; $uspsRequest->ship_address2 = ''; $uspsRequest->ship_city = ''; $uspsRequest->ship_state = ''; $uspsRequest->ship_zip = ''; */ $result = $uspsRequest->submit_request(); if (!empty($result)){ $xml = new SimpleXMLElement($result); }else{ die; } if(isset($xml->Address[0]->Error)) { echo ' Error Address 1';} if(isset($xml->Address[1]->Error)) { echo ' Error Address 2';} echo $xml->Address[0]->Address2 . ' ' . $xml->Address[0]->Address1 ; echo '<br />'; echo $xml->Address[0]->City. ' ' . $xml->Address[0]->State . ' ' . $xml->Address[0]->Zip5; echo '<br />'; echo $xml->Address[1]->Address2 . ' ' . $xml->Address[1]->Address1 ; echo '<br />'; echo $xml->Address[1]->City. ' ' . $xml->Address[1]->State . ' ' . $xml->Address[1]->Zip5; ?>
Related posts:
- php: Send xml/soap to Secured Server
- VB.net Code to Programatically Populate Address Fields in UPS Worldship
- php: Create Url Safe Encrypted String
- Block Spammers by IP address in WordPress
- jQuery and php Progressive Form Example







Very helpful script! Thanks.
Thank you sooo much!!
Great script
thanks alot
I just book marked your blog on Digg and StumbleUpon.I enjoy reading your commentaries.
just wanted to say thanks for sharing this-
Thanks for sharing such a wonderful code
Howdy! Do you know if they make any plugins to assist with SEO? I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good gains. If you know of any please share. Many thanks!
Awesome. I’m trying to get this incorporated into my e-Commerce store to make shopping easier
.
Appreciate it!