项目地址: https://github.com/gabrielbull/php-ups-api
composer require gabrielbull/ups-api
前提
PHP: UPS API, UPS 运费计算, UPS接口
PHP: UPS API, UPS 运费计算, UPS接口
用法:(完整整合类见最下方)
1. 地址验证 Address Validation Class (Street Level)
$address = new \Ups\Entity\Address();
$address->setAttentionName('Test Test');
$address->setBuildingName('Test');
$address->setAddressLine1('Address Line 1');
$address->setAddressLine2('Address Line 2');
$address->setAddressLine3('Address Line 3');
$address->setStateProvinceCode('NY');
$address->setCity('New York');
$address->setCountryCode('US');
$address->setPostalCode('10000');
$xav = new \Ups\AddressValidation($accessKey, $userId, $password);
$xav->activateReturnObjectOnValidate(); //This is optional
try {
$response = $xav->validate($address, $requestOption = \Ups\AddressValidation::REQUEST_OPTION_ADDRESS_VALIDATION, $maxSuggestion = 15);
} catch (Exception $e) {
var_dump($e);
}
其他选项:
if ($response->noCandidates()) {
//Do something clever and helpful to let the use know the address is invalid
}
if ($response->isAmbiguous()) {
$candidateAddresses = $response->getCandidateAddressList();
foreach($candidateAddresses as $address) {
//Present user with list of candidate addresses so they can pick the correct one
}
}
if ($response->isValid()) {
$validAddress = $response->getValidatedAddress();
//Show user validated address or update their address with the 'official' address
//Or do something else helpful...
}
2. 简单地址验证 Simple Address Validation Class
$address = new \Ups\Entity\Address();
$address->setStateProvinceCode('NY');
$address->setCity('New York');
$address->setCountryCode('US');
$address->setPostalCode('10000');
$av = new \Ups\SimpleAddressValidation($accessKey, $userId, $password);
try {
$response = $av->validate($address);
var_dump($response);
} catch (Exception $e) {
var_dump($e);
}
3. QuantumView Class
The QuantumView Class allow you to request a Quantum View Data subscription
$quantumView = new Ups\QuantumView($accessKey, $userId, $password);
try {
// Get the subscription for all events for the last hour
$events = $quantumView->getSubscription(null, (time() - 3600));
foreach($events as $event) {
// Your code here
echo $event->Type;
}
} catch (Exception $e) {
var_dump($e);
}
4. 追踪 Tracking Class
The Tracking Class allow you to track a shipment using the UPS Tracking API.
$tracking = new Ups\Tracking($accessKey, $userId, $password);
try {
$shipment = $tracking->track('TRACKING NUMBER');
foreach($shipment->Package->Activity as $activity) {
var_dump($activity);
}
} catch (Exception $e) {
var_dump($e);
}
Example using Reference Number
$tracking = new Ups\Tracking($accessKey, $userId, $password);
try {
$shipment = $tracking->trackByReference('REFERENCE NUMBER');
foreach($shipment->Package->Activity as $activity) {
var_dump($activity);
}
} catch (Exception $e) {
var_dump($e);
}
Example using Reference Number with additional parameters
$tracking = new Ups\Tracking($accessKey, $userId, $password);
$tracking->setShipperNumber('SHIPPER NUMBER');
$beginDate = new \DateTime('2016-01-01');
$endDate = new \DateTime('2016-01-31');
$tracking->setBeginDate($beginDate);
$tracking->setEndDate($endDate);
try {
$shipment = $tracking->trackByReference('REFERENCE NUMBER');
foreach($shipment->Package->Activity as $activity) {
var_dump($activity);
}
} catch (Exception $e) {
var_dump($e);
}
5. 价格运算 Rate Class
The Rate Class allow you to get shipment rates using the UPS Rate API.
$rate = new Ups\Rate(
$accessKey,
$userId,
$password
);
try {
$shipment = new \Ups\Entity\Shipment();
$shipperAddress = $shipment->getShipper()->getAddress();
$shipperAddress->setPostalCode('99205');
$address = new \Ups\Entity\Address();
$address->setPostalCode('99205');
$shipFrom = new \Ups\Entity\ShipFrom();
$shipFrom->setAddress($address);
$shipment->setShipFrom($shipFrom);
$shipTo = $shipment->getShipTo();
$shipTo->setCompanyName('Test Ship To');
$shipToAddress = $shipTo->getAddress();
$shipToAddress->setPostalCode('99205');
$package = new \Ups\Entity\Package();
$package->getPackagingType()->setCode(\Ups\Entity\PackagingType::PT_PACKAGE);
$package->getPackageWeight()->setWeight(10);
// if you need this (depends of the shipper country)
$weightUnit = new \Ups\Entity\UnitOfMeasurement;
$weightUnit->setCode(\Ups\Entity\UnitOfMeasurement::UOM_KGS);
$package->getPackageWeight()->setUnitOfMeasurement($weightUnit);
$dimensions = new \Ups\Entity\Dimensions();
$dimensions->setHeight(10);
$dimensions->setWidth(10);
$dimensions->setLength(10);
$unit = new \Ups\Entity\UnitOfMeasurement;
$unit->setCode(\Ups\Entity\UnitOfMeasurement::UOM_IN);
$dimensions->setUnitOfMeasurement($unit);
$package->setDimensions($dimensions);
$shipment->addPackage($package);
var_dump($rate->getRate($shipment));
} catch (Exception $e) {
var_dump($e);
}
6. 价格和时间计算 RateTimeInTransit Class
The RateTimeInTransit Class allow you to get shipment rates like the Rate Class, but the response will also include TimeInTransit data.
$rate = new Ups\RateTimeInTransit(
$accessKey,
$userId,
$password
);
try {
$shipment = new \Ups\Entity\Shipment();
$shipperAddress = $shipment->getShipper()->getAddress();
$shipperAddress->setPostalCode('99205');
$address = new \Ups\Entity\Address();
$address->setPostalCode('99205');
$shipFrom = new \Ups\Entity\ShipFrom();
$shipFrom->setAddress($address);
$shipment->setShipFrom($shipFrom);
$shipTo = $shipment->getShipTo();
$shipTo->setCompanyName('Test Ship To');
$shipToAddress = $shipTo->getAddress();
$shipToAddress->setPostalCode('99205');
$package = new \Ups\Entity\Package();
$package->getPackagingType()->setCode(\Ups\Entity\PackagingType::PT_PACKAGE);
$package->getPackageWeight()->setWeight(10);
// if you need this (depends of the shipper country)
$weightUnit = new \Ups\Entity\UnitOfMeasurement;
$weightUnit->setCode(\Ups\Entity\UnitOfMeasurement::UOM_KGS);
$package->getPackageWeight()->setUnitOfMeasurement($weightUnit);
$dimensions = new \Ups\Entity\Dimensions();
$dimensions->setHeight(10);
$dimensions->setWidth(10);
$dimensions->setLength(10);
$unit = new \Ups\Entity\UnitOfMeasurement;
$unit->setCode(\Ups\Entity\UnitOfMeasurement::UOM_IN);
$dimensions->setUnitOfMeasurement($unit);
$package->setDimensions($dimensions);
$shipment->addPackage($package);
$deliveryTimeInformation = new \Ups\Entity\DeliveryTimeInformation();
$deliveryTimeInformation->setPackageBillType(\Ups\Entity\DeliveryTimeInformation::PBT