USPS API V3 Implementation in PHP: How to send a mail using USPS API V3 in PHP?
To send a mail using USPS API V3 in PHP, you need to follow these steps:
-
Sign up for a USPS Web Tools account: Before you start implementing USPS API V3 in PHP, you need to sign up for a USPS Web Tools account. You can sign up for a free account on the USPS website. Once you have signed up, you will receive an API key that you will use to authenticate your API requests.
-
Install PHP USPS library: To simplify the process of sending mail using USPS API V3 in PHP, you can use a PHP library called PHP USPS. This library provides a wrapper around the USPS API and makes it easier to send mail using PHP. You can download the library from the following link: https://github.com/josegonzalez/php-usps
-
Configure the library: Once you have downloaded and installed the PHP USPS library, you need to configure it with your USPS Web Tools account credentials. Open the config.php file located in the library's root directory and update the following variables:
'api_user' => 'your_api_username',
'api_password' => 'your_api_password',
'api_key' => 'your_api_key',
'api_url' => 'https://production.shippingapi.com/ShippingAPI.dll',
'sandbox_api_url' => 'https://sandbox.shippingapi.com/ShippingAPI.dll',
'sandbox_api_key' => 'your_sandbox_api_key',
Replace 'your_api_username', 'your_api_password', 'your_api_key', and 'your_sandbox_api_key' with your actual USPS Web Tools account credentials.
- Send the mail: Now that you have configured the library, you can send mail using USPS API V3 in PHP. Here's an example of how to send a mail using the library:
require_once 'vendor/autoload.php';
use USPS\ShippingApiClient;
use USPS\Shipment\Address;
use USPS\Shipment\Package;
use USPS\Shipment\Shipment;
use USPS\Shipment\ShipmentRequest;
use USPS\Shipment\ShipmentResponse;
$client = new ShippingApiClient();
// Sender address
$senderAddress = new Address();
$senderAddress->setStreet1('123 Main St')
->setCity('Anytown')
->setState('CA')
->setZip5('12345')
->setCountry('US');
// Recipient address
$recipientAddress = new Address();
$recipientAddress->setStreet1('456 Oak St')
->setCity('Othertown')
->setState('NY')
->setZip5('56789')
->setCountry('US');
// Package details
$package = new Package();
$package->setWeightValue(2.0)
->setWeightUnits('LB')
->setServiceType('PRIORITY')
->setContainerType('YOUR_PACKAGING')
->setSize('RECTANGULAR')
->setLength('12')
->setWidth('6')
->setHeight('3');
// Shipment details
$shipment = new Shipment();
$shipment->setShipperAddress($senderAddress)
->setRecipientAddress($recipientAddress)
->addPackage($package);
// Send the mail
$shipmentRequest = new ShipmentRequest();
$shipmentRequest->setShipment($shipment);
$shipmentResponse = $client->shipment->create($shipmentRequest);
// Print the shipping label
echo $shipmentResponse->getLabel();
Replace the sender and recipient addresses with the actual addresses, and update the package details with the weight and dimensions of your mail piece. The 'PRIORITY' service type in the example corresponds to Priority Mail in the USPS service offerings. You can change it to any other service type supported by USPS.
Once you have sent the mail using USPS API V3 in PHP, the library will return a response containing the shipping label, which you can print or save as a file.