mmediasoftwarelab/usps-oauth-php 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mmediasoftwarelab/usps-oauth-php

Composer 安装命令:

composer require mmediasoftwarelab/usps-oauth-php

包简介

Modern PHP library for USPS OAuth 2.0 API - Shipping rates, labels, and tracking for the 2026+ API

README 文档

README

Latest Version PHP Version License Tests Coverage

Modern PHP library for the USPS OAuth 2.0 API (2026+). Get real-time shipping rates, generate labels, and track packages using the latest USPS Web Tools API v3.

Why this library?

  • OAuth 2.0 - Uses the new USPS API (2026+ ready)
  • Modern PHP - PHP 8.1+ with type safety
  • Framework Agnostic - Works with any PHP project
  • PSR Compatible - PSR-4 autoloading, PSR-3 logging
  • Well Tested - Comprehensive unit tests
  • Production Ready - Used in commercial applications

Installation

composer require mmediasoftwarelab/usps-oauth-php

Requirements

  • PHP 8.1 or higher
  • ext-json
  • ext-curl
  • ext-openssl (for HTTPS requests)
  • ext-mbstring (recommended)
  • Valid USPS Business Account with API credentials

Getting USPS API Credentials

  1. Create a USPS Business Account at USPS.com
  2. Request API access through the USPS Web Tools portal
  3. You'll receive a Client ID and Client Secret for OAuth authentication

Environment Setup

For development and testing, create a .env file:

cp .env.example .env

Add your credentials:

USPS_CLIENT_ID=your-client-id-here
USPS_CLIENT_SECRET=your-client-secret-here
USPS_SANDBOX=true

Note: Never commit .env to version control. It's already in .gitignore.

Quick Start

<?php
require 'vendor/autoload.php';

use MMedia\USPS\Client;
use MMedia\USPS\Rates\DomesticRates;

// Initialize client
$client = new Client(
    clientId: 'your-client-id',
    clientSecret: 'your-client-secret',
    sandbox: true // Use sandbox for testing
);

// Get domestic shipping rates
$domesticRates = new DomesticRates($client);

$rate = $domesticRates->getRate(
    originZip: '90210',
    destinationZip: '10001',
    weightLbs: 2.5,
    length: 12,
    width: 8,
    height: 6,
    serviceType: 'USPS_GROUND_ADVANTAGE'
);

echo "Shipping cost: $" . $rate->getTotalPrice();

Features

Domestic Shipping Rates

use MMedia\USPS\Rates\DomesticRates;
use MMedia\USPS\Enums\DomesticServiceType;

$rates = new DomesticRates($client);

// Get rate for specific service
$rate = $rates->getRate(
    originZip: '90210',
    destinationZip: '10001',
    weightLbs: 2.5,
    length: 12,
    width: 8,
    height: 6,
    serviceType: DomesticServiceType::GROUND_ADVANTAGE
);

// Or get all available services
$allRates = $rates->getAllRates(
    originZip: '90210',
    destinationZip: '10001',
    weightLbs: 2.5,
    length: 12,
    width: 8,
    height: 6
);

foreach ($allRates as $service => $rate) {
    echo "{$service}: $" . $rate->getTotalPrice() . "\n";
}

International Shipping Rates

use MMedia\USPS\Rates\InternationalRates;
use MMedia\USPS\Enums\InternationalServiceType;

$rates = new InternationalRates($client);

$rate = $rates->getRate(
    originZip: '90210',
    destinationCountry: 'CA', // Canada
    weightLbs: 2.5,
    length: 12,
    width: 8,
    height: 6,
    serviceType: InternationalServiceType::PRIORITY_MAIL_INTERNATIONAL
);

Supported Services

Domestic:

  • USPS Ground Advantage
  • Priority Mail
  • Priority Mail Express
  • Media Mail
  • First-Class Package

International:

  • Priority Mail International
  • Priority Mail Express International
  • First-Class Package International

Configuration

Sandbox vs Production

// Development/Testing
$client = new Client(
    clientId: 'sandbox-client-id',
    clientSecret: 'sandbox-secret',
    sandbox: true
);

// Production
$client = new Client(
    clientId: 'production-client-id',
    clientSecret: 'production-secret',
    sandbox: false
);

Custom HTTP Client

use MMedia\USPS\Http\CurlHttpClient;

$httpClient = new CurlHttpClient(
    timeout: 30,
    connectTimeout: 10
);

$client = new Client(
    clientId: 'your-client-id',
    clientSecret: 'your-client-secret',
    httpClient: $httpClient
);

Logging

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

$logger = new Logger('usps');
$logger->pushHandler(new StreamHandler('usps.log', Logger::DEBUG));

$client = new Client(
    clientId: 'your-client-id',
    clientSecret: 'your-client-secret',
    logger: $logger
);

Framework Integration

Laravel

// In a service provider
use MMedia\USPS\Client;

$this->app->singleton(Client::class, function ($app) {
    return new Client(
        clientId: config('services.usps.client_id'),
        clientSecret: config('services.usps.client_secret'),
        sandbox: config('services.usps.sandbox', true),
        logger: $app->make('log')
    );
});

Symfony

# config/services.yaml
services:
  MMedia\USPS\Client:
    arguments:
      $clientId: "%env(USPS_CLIENT_ID)%"
      $clientSecret: "%env(USPS_CLIENT_SECRET)%"
      $sandbox: "%env(bool:USPS_SANDBOX)%"
      $logger: "@logger"

WordPress/WooCommerce

See the WordPress Adapter for WooCommerce integration.

Error Handling

use MMedia\USPS\Exceptions\AuthenticationException;
use MMedia\USPS\Exceptions\RateException;
use MMedia\USPS\Exceptions\ValidationException;

try {
    $rate = $domesticRates->getRate(
        originZip: '90210',
        destinationZip: '10001',
        weightLbs: 2.5,
        length: 12,
        width: 8,
        height: 6,
        serviceType: 'USPS_GROUND_ADVANTAGE'
    );
} catch (AuthenticationException $e) {
    // OAuth token failed - check credentials
    error_log("Authentication failed: " . $e->getMessage());
} catch (ValidationException $e) {
    // Invalid input data
    error_log("Validation error: " . $e->getMessage());
} catch (RateException $e) {
    // Rate calculation failed
    error_log("Rate error: " . $e->getMessage());
}

Testing

This library has comprehensive test coverage with unit and integration tests.

# Run all tests
composer test

# Run with coverage report
vendor/bin/phpunit --coverage-html coverage

# Run only unit tests
vendor/bin/phpunit --testsuite="USPS OAuth PHP Test Suite"

# Run integration tests (requires USPS credentials)
USPS_CLIENT_ID=xxx USPS_CLIENT_SECRET=yyy vendor/bin/phpunit --testsuite=integration

# Run static analysis (PHPStan level 8)
composer phpstan

# Check code style (PSR-12)
composer phpcs

Test Coverage: 90%+ code coverage with automated CI/CD testing on PHP 8.1, 8.2, and 8.3.

See TESTING.md for detailed testing documentation.

Documentation

API Documentation

For detailed USPS API documentation, visit:

Examples

Check the examples directory for:

  • Basic usage
  • Laravel integration
  • Symfony integration
  • WordPress/WooCommerce adapter
  • Custom HTTP client
  • Error handling patterns

Premium Features 💎

Looking for advanced features? Check out USPS OAuth PHP Pro:

  • 🏷️ Label Generation - PDF, PNG, and ZPL formats
  • 📦 Package Tracking - Real-time tracking with webhooks
  • 📊 Multi-Carrier - Compare rates across USPS, UPS, FedEx
  • Rate Caching - Redis/Memcached integration
  • 🔄 Batch Operations - Process thousands of shipments
  • 💳 Commercial Support - Priority support and SLA

Learn more about Pro features →

Framework Integrations

  • WooCommerce Plugin - $149
  • Laravel Package - Coming Q2 2026
  • Symfony Bundle - Coming Q2 2026
  • Magento Extension - Coming Q3 2026
  • Shopify App - Coming Q3 2026

Contributing

We welcome contributions! Please see CONTRIBUTING.md for details.

Security

If you discover any security issues, please email security@mmediasoftwarelab.com instead of using the issue tracker.

License

The MIT License (MIT). Please see LICENSE for more information.

Credits

Developed by M Media

Support

mmediasoftwarelab/usps-oauth-php 适用场景与选型建议

mmediasoftwarelab/usps-oauth-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 01 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「ecommerce」 「api」 「oauth」 「oauth2」 「shipping」 「rates」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 mmediasoftwarelab/usps-oauth-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 mmediasoftwarelab/usps-oauth-php 我们能提供哪些服务?
定制开发 / 二次开发

基于 mmediasoftwarelab/usps-oauth-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 32
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-11