PHP Classes

How to Use the PHP Address Validation Class to Validate and Parse Addresses using USPS API v3 - PHP USPS API Address Validation package blog

Recommend this page to a friend!
  All package blogs All package blogs   PHP USPS API Address Validation PHP USPS API Address Validation   Blog PHP USPS API Address Validation package blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Use the PHP Ad...  
  Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  

Author:

Updated on: 2023-12-20

Posted on: 2023-12-20

Package: PHP USPS API Address Validation

Sending physical product orders requires that the destination address be correct. Address spelling mistakes may be expensive because shipping costs must be paid again when products bounce due to incorrect destination addresses.

One way to avoid address spelling mistakes is to help the customer verify the address before shipping the product.

Read this article to see an example code and learn how to use the USPS Address Validator package to verify an address before shipping a product using the USPS service.




Loaded Article

In this article you will learn:

1. What is the USPS Address Validation API

2. How Can You Use the USPS Address Validation API to Check An Address Entered by a Customer of a Company That Uses PHP to Implement a E-Commerce Site

3. How to Download or Install the PHP Address Validation Class with PHP Composer



1. What is the USPS Address Validation API

USPS is a well-known United States postal service company.

USPS provides several types of API to help individuals or companies ship their products to locations in the United States.

One of the APIs USPS provides allows developers to verify addresses to check their validity.

2. How Can You Use the USPS Address Validation API to Check An Address Entered by a Customer of a Company That Uses PHP to Implement a E-Commerce Site

The USPS Address Validator PHP package is designed to provide a class with functions to call upon the USPS to validate an address.

I have updated this PHP package to version 2.0 as it now uses the USPS API v3 which now has a different authentication mechanism, and new API endpoints. It additionally removes deprecated code from PHP 5, and now has a minimum PHP 7.4 required. It was tested in PHP 8.2.

Here is a an example PHP code sample to let you see how to use this package in practice.

require('USPS.php');

use 
RedWebDev\src\USPS;


// insert your api key from USPS
$uspsZip = new USPS('Consumer Key','Consumer Secret');


// address line is required
$address '1600 Amphitheatre Parkway';
$city 'Mountain View';

//looking for the two character state
$state 'CA';

$return $uspsZip->getNormalized($address$city$state);

echo 
"<ul>";
echo 
"<li>Address: " $return['address']['streetAddress'];
echo 
"<li>Address 2: " $return['address']['secondaryAddress'];
echo 
"<li>City: " $return['address']['city'];
echo 
"<li>State: " $return['address']['state'];
echo 
"<li>Zip: " $return['address']['ZIPCode'];

3. How to Download or Install the PHP Address Validation Class with PHP Composer

This package can be downloaded by accessing the Download tab of the PHP Address Validation package page.

If you use the PHP Composer tool to install packages in your PHP projects, you can follow instructions also form the Download tab or the PHP Address Validation page to update your projects composer.json file to install this package.




You need to be a registered user or login to post a comment

Login Immediately with your account on:



Comments:

No comments were submitted yet.



  Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  
  All package blogs All package blogs   PHP USPS API Address Validation PHP USPS API Address Validation   Blog PHP USPS API Address Validation package blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Use the PHP Ad...