Using USPS APIs in PHP

Do you want to know how to use the USPS API in PHP? We'll show you how, and share step-by-step examples with you. (You may also want to check out our video tutorial on how to use the USPS APIs in PHP).

In this article, we'll discuss:

Creating a USPS Account

Before you can use the USPS APIs, you will need to register with the USPS to receive a . This ID is used in the sample code below where is mentioned.

Types of USPS APIs

  • There are three USPS APIs available:
    • Address Standardization: validates an address and returns the accepted standardized version of it with other helpful metadata.
    • ZIP Code Lookup: a simplified version of the standardization API and returns the ZIP Code and ZIP+4.
    • City/State Lookup: returns the city and state associated with a specified zipcode.
  • You can only make up to 5 requests per API call.
  • XML is used for the Request and Response docs. (Bummer)

Code Example

Note that this sample requires a (see Register above).

Address Standardization Sample Code

<?php
$request_doc_template = <<<EOT
<?xml version="1.0"?>
<AddressValidateRequest USERID="">
	<Revision>1</Revision>
	<Address ID="0">
		<Address1>2335 S State</Address1>
		<Address2>Suite 300</Address2>
		<City>Provo</City>
		<State>UT</State>
		<Zip5>84604</Zip5>
		<Zip4/>
	</Address>
</AddressValidateRequest>
EOT;

// prepare xml doc for query string
$doc_string = preg_replace('/[\t\n]/', '', $request_doc_template);
$doc_string = urlencode($doc_string);

$url = "http://production.shippingapis.com/ShippingAPI.dll?API=Verify&XML=" . $doc_string;
echo $url . "\n\n";

// perform the get
$response = file_get_contents($url);

$xml=simplexml_load_string($response) or die("Error: Cannot create object");
print_r($xml);

echo "Address1: " . $xml->Address->Address1 . "\n";
echo "Address2: " . $xml->Address->Address2 . "\n";
echo "City: " . $xml->Address->City . "\n";
echo "State: " . $xml->Address->State . "\n";
echo "Zip5: " . $xml->Address->Zip5 . "\n";

?>

Code Discussion

The first section is the XML request document. In this sample we cheated a bit and just did a multi-line string.
In the XML document string, you will need to specify your USERID that you received when you registered with USPS.

$request_doc_template = <<<EOT
<?xml version="1.0"?>
<AddressValidateRequest USERID="">
	<Revision>1</Revision>
	<Address ID="0">
		<Address1>2335 S State</Address1>
		<Address2>Suite 300</Address2>
		<City>Provo</City>
		<State>UT</State>
		<Zip5>84604</Zip5>
		<Zip4/>
	</Address>
</AddressValidateRequest>
EOT;

Next, we prepared the xml document string so it can be used on the URL query string. We did this by removing all tabs and newlines, then url encoding the document.

We then build the complete URL and echo it to the screen for debugging purposes.

Finally, we make the HTTP GET call using file_get_contents().

// prepare xml doc for query string
$doc_string = preg_replace('/[\t\n]/', '', $request_doc_template);
$doc_string = urlencode($doc_string);

$url = "http://production.shippingapis.com/ShippingAPI.dll?API=Verify&XML=" . $doc_string;
echo $url . "\n\n";

// perform the get
$response = file_get_contents($url);

The last section is parsing the response into a true XML document and printing out the entire document so you can see all the available fields.

Then we output a few of the returned fields to demonstrate how to access XML elements.

$xml=simplexml_load_string($response) or die("Error: Cannot create object");
print_r($xml);

echo "Address1: " . $xml->Address->Address1 . "\n";
echo "Address2: " . $xml->Address->Address2 . "\n";
echo "City: " . $xml->Address->City . "\n";
echo "State: " . $xml->Address->State . "\n";
echo "Zip5: " . $xml->Address->Zip5 . "\n";

Alternative Address Validation APIs

Smarty (formerly SmartyStreets) also provides an API set to validate addresses. So why would Smarty assist in using an API that isn't theirs?

The answer is simple - we care. We want you to get your job done, and if you really want to use the USPS API, then hopefully this article will help you.

However, we think that once you see how much easier the Smarty PHP SDK is to use, you will want to use it instead of the USPS APIs. And, if you do, you'll get excellent customer service in the process. And of course we won't even mention that Smarty APIs are blazingly fast!

Take a moment and compare the sample code above with the Smarty PHP SDK sample code.

When you compare the USPS API experience with the experience of using the various Smarty APIs, you'll see that Smarty wins, every time. From the easy-to-use JSON response, to the stellar support, and the faster response times, and the fact that you can make up to 100 requests per API call to Smarty APIs, Smarty wins everytime.

Try Smarty for yourself with a free demo account. You'll be glad that you did.

The leader in location data intelligence

Ready to get started?