PHP code example of asteriskpound / usps-php-api

1. Go to this page and download the library: Download asteriskpound/usps-php-api library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

asteriskpound / usps-php-api example snippets



ColeThorsen\USPS\USPS;

// Initialize the USPS client
$usps = new USPS('your-consumer-key', 'your-consumer-secret', [
    'testMode' => true, // Use false for production
]);

$usps = new USPS($consumerKey, $consumerSecret, [
    'testMode'         => true,           // Use test environment
    'timeout'          => 30,             // Request timeout in seconds
    'validateRequests' => true,           // Enable request validation
    'cache'            => $cachePool,     // PSR-6 cache instance (optional)
]);

use ColeThorsen\USPS\Enums\AddressType;

// Validate a single address
$address = [
    'streetAddress' => '1234 Main St',
    'city' => 'Los Angeles',
    'state' => 'CA',
    'ZIPCode' => '90210'
];

$result = $usps->addresses->validate($address);

// Validate multiple addresses
$addresses = [$address1, $address2, $address3];
$results = $usps->addresses->validateBatch($addresses);

use ColeThorsen\USPS\Enums\MailClass;
use ColeThorsen\USPS\Enums\ProcessingCategory;
use ColeThorsen\USPS\Enums\RateIndicator;
use ColeThorsen\USPS\Enums\PriceType;

// Get base shipping rates
$params = [
    'originZIPCode'      => '90210',
    'destinationZIPCode' => '10001',
    'weight'             => 2.5,
    'length'             => 12.0,
    'width'              => 8.0,
    'height'             => 3.0,
    'mailClass'          => MailClass::PRIORITY_MAIL->value,
    'processingCategory' => ProcessingCategory::MACHINABLE->value,
    'rateIndicator'      => RateIndicator::SINGLE_PIECE->value,
    'priceType'          => PriceType::RETAIL->value,
    'mailingDate'        => '2024-01-15',
];

$rates = $usps->domesticPrices->baseRates($params);

// Get rates with extra services
use ColeThorsen\USPS\Enums\ExtraService;

$params['extraServices'] = [
    ExtraService::USPS_TRACKING_ELECTRONIC->value,
    ExtraService::SIGNATURE_CONFIRMATION->value,
];

$ratesWithServices = $usps->domesticPrices->extraServiceRates($params);

use ColeThorsen\USPS\Enums\DestinationEntryFacilityType;

$params = [
    'originZIPCode'                => '90210',
    'destinationCountryCode'       => 'GB',
    'foreignPostalCode'            => 'SW1A 1AA',
    'destinationEntryFacilityType' => DestinationEntryFacilityType::INTERNATIONAL_SERVICE_CENTER->value,
    'weight'                       => 2.0,
    'length'                       => 12.0,
    'width'                        => 8.0,
    'height'                       => 3.0,
    'mailClass'                    => MailClass::PRIORITY_MAIL_INTERNATIONAL->value,
    'processingCategory'           => ProcessingCategory::MACHINABLE->value,
    'rateIndicator'                => RateIndicator::SINGLE_PIECE->value,
    'priceType'                    => PriceType::RETAIL->value,
    'mailingDate'                  => '2024-01-15',
];

$internationalRates = $usps->internationalPrices->baseRates($params);

// Find USPS locations
$params = [
    'ZIPCode' => '90210',
    'radius'  => 10,
];

$locations = $usps->locations->search($params);

// Get dropoff locations
$dropoffParams = [
    'ZIPCode' => '90210',
    'radius'  => 5,
];

$dropoffLocations = $usps->locations->dropoffLocations($dropoffParams);

use ColeThorsen\USPS\Exceptions\ValidationException;
use ColeThorsen\USPS\Exceptions\TechnicalException;
use ColeThorsen\USPS\Exceptions\USPSException;

try {
    $result = $usps->addresses->validate($address);
} catch (ValidationException $e) {
    // Request validation failed
    echo "Validation Error: " . $e->getMessage();
} catch (TechnicalException $e) {
    // API or technical error
    echo "Technical Error: " . $e->getMessage();
} catch (USPSException $e) {
    // General USPS API error
    echo "USPS Error: " . $e->getMessage();
}

use Symfony\Component\Cache\Adapter\FilesystemAdapter;

$cache = new FilesystemAdapter();
$usps = new USPS($key, $secret, ['cache' => $cache]);

// Disable validation for faster requests (not recommended for production)
$usps = new USPS($key, $secret, ['validateRequests' => false]);

$usps = new USPS($key, $secret, ['timeout' => 60]);
bash
composer