jamesvweston/usps-php

USPS API 的 PHP 封装器

dev-master 2016-08-08 18:24 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:42:11 UTC


README

基于 https://www.usps.com/business/web-tools-apis/address-information-api.htm 的 USPS API 的面向对象且文档完善的 PHP 封装器

特性

  • USPS API 的面向对象 PHP 封装器
  • 您只需要提供 USPS USER_ID 和环境选择(生产或开发)
  • 良好的文档,PHP 文档块,易于使用
  • 完整的 PHP 单元测试套件

活跃的 API 集成

  • 地址验证
  • 使用邮政编码查找城市和州
  • 使用街道地址、城市和州查找邮政编码

地址查找示例

use jamesvweston\USPS\USPSClient;
use jamesvweston\USPS\Models\Address;
use jamesvweston\USPS\Models\Requests\AddressValidateRequest;

//  Instantiate the client with the path that the .env is located
$uspsClient             = new USPSClient('./');
$addressApi             = $uspsClient->getAddressApi();

//  Construct an address object to validate
$address                = new Address();
$address->setAddress2('111 W Harbor Dr');
$address->setCity('San Diego');
$address->setState('CA');
$address->setZip5('92101');

//  Construct the request
$addressValidateRequest = new AddressValidateRequest();

//  Add the address to the request
$addressValidateRequest->addAddress($address);

//  Make the request
$response               = $addressApi->validateAddress($addressValidateRequest);

安装

composer require jamesvweston/usps-php

配置

以下变量是 USPSClient 所必需的

  • USPS_USER_ID - 由 USPS 提供的用户 ID
  • USPS_ENVIRONMENT - 生产或开发

使用 .env 文件实例化 USPSClient 的示例

use jamesvweston\USPS\USPSClient;

//  Instantiate the client with the path that the .env is located
$uspsClient             = new USPSClient('./');

使用数组实例化 USPSClient 的示例

use jamesvweston\USPS\USPSClient;

//  Build the array
$config = [
    'USPS_USER_ID'      => 'xxxxx',
    'USPS_ENVIRONMENT'  => 'production',
];

//  Instantiate the USPSClient with the $config array
$uspsClient             = new USPSClient($config);

单元测试

从命令行运行以下命令

vendor/bin/phpunit -c phpunit.xml