bkuhl/simple-ups

通过UPS API获取运费,跟踪包裹和验证地址

1.0 2015-04-19 16:39 UTC

This package is auto-updated.

Last update: 2024-09-10 04:32:59 UTC


README

一个易于使用的PHP UPS库,用于跟踪、运费和地址验证

Total Downloads License Scrutinizer Code Quality

README 内容

## 功能
  • 地址验证 - 确保地址有效,再由您的应用程序接受
  • 地址纠正 - 如果地址无效,我们将帮助您纠正它
  • 跟踪包裹 - 查看当前状态、最近活动、交付要求(签名等)、保险详情等
  • 运费 - 获取包裹的运费估算
## 安装

您可以通过运行以下命令通过Composer安装库:

composer require bkuhl/simple-ups:1.*

1.*版本将保持与PHP 5.3的兼容性。《dev-master》将为未来的2.*版本增加PHP版本要求。

## Usage

SimpleUPS is currently only available in a static context with the following methods:

  • SimpleUPS::getRates()
  • SimpleUPS::isValidRegion()
  • SimpleUPS::getSuggestedRegions()
  • SimpleUPS::trackByTrackingNumber()
  • SimpleUPS::isValidAddress()
  • SimpleUPS::getCorrectedAddress()
  • SimpleUPS::getSuggestedAddresses()
  • SimpleUPS::setAuthentication()
  • SimpleUPS::getAccountNumber()
  • SimpleUPS::getAccessLicenseNumber()
  • SimpleUPS::getPassword()
  • SimpleUPS::getUserId()
  • SimpleUPS::setShipper()
  • SimpleUPS::getShipper()
  • SimpleUPS::setCurrencyCode()
  • SimpleUPS::setDebug()
  • SimpleUPS::getDebugOutput()
### Address Validation

Validating an address can be useful to ensure an address that a user provides can be shipped to.

$address = new Address(); $address->setStreet('1001 North Alameda Street'); $address->setStateProvinceCode('CA'); $address->setCity('Los Angeles'); $address->setPostalCode(90012); $address->setCountryCode('US'); try { var_dump(UPS::isValidAddress($address)); // true } catch(Exception $e) { //unable to validate address }
### Region Validation

If an address fails, validating the region can help you determine if the city, state and zip is valid even if the street address isn't.

$address = new Address(); $address->setStreet('xx North Alameda Street'); $address->setStateProvinceCode('CA'); $address->setCity('Los Angeles'); $address->setPostalCode(90012); $address->setCountryCode('US'); try { if (!UPS::isValidAddress($address)) var_dump(UPS::isValidRegion($address)); // true } catch(Exception $e) { //unable to validate region or address }
### Tracking Shipments

Tracking numbers may contain multiple shipments, and shipments may contain multiple packages, and activity is associated with packages.

try { /* @var $shipment \SimpleUPS\Track\SmallPackage\Shipment */ foreach (UPS::trackByTrackingNumber('1Z4861WWE194914215') as $shipment) foreach ($shipment->getPackages() as $package) foreach ($package->getActivity() as $activity) if ($activity->getStatusType()->isDelivered()) echo 'DELIVERED'; } catch (TrackingNumberNotFoundException $e) { //Tracking number does not exist } catch (Exception $e) { //Unable to track package } var_dump(UPS::isValidAddress($address)); // false
### Fetching Rates
try { //set shipper $fromAddress = new \SimpleUPS\InstructionalAddress(); $fromAddress->setAddressee('Mark Stevens'); $fromAddress->setStreet('10571 Pico Blvd'); $fromAddress->setStateProvinceCode('CA'); $fromAddress->setCity('Los Angeles'); $fromAddress->setPostalCode(90064); $fromAddress->setCountryCode('US'); $shipper = new \SimpleUPS\Shipper(); $shipper->setNumber('xxxxxxx'); $shipper->setAddress($fromAddress); UPS::setShipper($shipper); //define a shipping destination $shippingDestination = new \SimpleUPS\InstructionalAddress(); $shippingDestination->setStreet('220 Bowery'); $shippingDestination->setStateProvinceCode('NY'); $shippingDestination->setCity('New York'); $shippingDestination->setPostalCode(10453); $shippingDestination->setCountryCode('US'); //define a package, we could specify the dimensions of the box if we wanted a more accurate estimate $package = new \SimpleUPS\Rates\Package(); $package->setWeight('7'); $shipment = new \SimpleUPS\Rates\Shipment(); $shipment->setDestination($shippingDestination); $shipment->addPackage($package); echo 'Rates: '; echo '<ul>'; foreach (UPS::getRates($shipment) as $shippingMethod) echo '<li>'.$shippingMethod->getService()->getDescription().' ($'.$shippingMethod->getTotalCharges().')</li>'; echo '</ul>'; } catch (Exception $e) { //doh, something went wrong echo 'Failed: ('.get_class($e).') '.$e->getMessage().'<br/>'; echo 'Stack trace:<br/><pre>'.$e->getTraceAsString().'</pre>'; }