项目地址: 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_NON_DOCUMENT);

$pickup = new \Ups\Entity\Pickup();

$pickup->setDate("20170520");

$pickup->setTime("160000");

$shipment->setDeliveryTimeInformation($deliveryTimeInformation);

var_dump($rate->shopRatesTimeInTransit($shipment));

} catch (Exception $e) {

var_dump($e);

}

7. 时间估算 TimeInTransit Class

The TimeInTransit Class allow you to get all transit times using the UPS TimeInTransit API.

$timeInTransit = new Ups\TimeInTransit($access, $userid, $passwd);

try {

$request = new \Ups\Entity\TimeInTransitRequest;

// Addresses

$from = new \Ups\Entity\AddressArtifactFormat;

$from->setPoliticalDivision3('Amsterdam');

$from->setPostcodePrimaryLow('1000AA');

$from->setCountryCode('NL');

$request->setTransitFrom($from);

$to = new \Ups\Entity\AddressArtifactFormat;

$to->setPoliticalDivision3('Amsterdam');

$to->setPostcodePrimaryLow('1000AA');

$to->setCountryCode('NL');

$request->setTransitTo($to);

// Weight

$shipmentWeight = new \Ups\Entity\ShipmentWeight;

$shipmentWeight->setWeight($totalWeight);

$unit = new \Ups\Entity\UnitOfMeasurement;

$unit->setCode(\Ups\Entity\UnitOfMeasurement::UOM_KGS);

$shipmentWeight->setUnitOfMeasurement($unit);

$request->setShipmentWeight($shipmentWeight);

// Packages

$request->setTotalPackagesInShipment(2);

// InvoiceLines

$invoiceLineTotal = new \Ups\Entity\InvoiceLineTotal;

$invoiceLineTotal->setMonetaryValue(100.00);

$invoiceLineTotal->setCurrencyCode('EUR');

$request->setInvoiceLineTotal($invoiceLineTotal);

// Pickup date

$request->setPickupDate(new DateTime);

// Get data

$times = $timeInTransit->getTimeInTransit($request);

foreach($times->ServiceSummary as $serviceSummary) {

var_dump($serviceSummary);

}

} catch (Exception $e) {

var_dump($e);

}

8. Locator Class

The Locator class allows you to search for UPS Access Point locations.

$locatorRequest = new \Ups\Entity\LocatorRequest;

$originAddress = new \Ups\Entity\OriginAddress;

$address = new \Ups\Entity\AddressKeyFormat;

$address->setCountryCode('NL');

$originAddress->setAddressKeyFormat($address);

$geocode = new \Ups\Entity\GeoCode;

$geocode->setLatitude(52.0000);

$geocode->setLongitude(4.0000);

$originAddress->setGeoCode($geocode);

$locatorRequest->setOriginAddress($originAddress);

$translate = new \Ups\Entity\Translate;

$translate->setLanguageCode('ENG');

$locatorRequest->setTranslate($translate);

$acccessPointSearch = new \Ups\Entity\AccessPointSearch;

$acccessPointSearch->setAccessPointStatus(\Ups\Entity\AccessPointSearch::STATUS_ACTIVE_AVAILABLE);

$locationSearch = new \Ups\Entity\LocationSearchCriteria;

$locationSearch->setAccessPointSearch($acccessPointSearch);

$locationSearch->setMaximumListSize(25);

$locatorRequest->setLocationSearchCriteria($locationSearch);

$unitOfMeasurement = new \Ups\Entity\UnitOfMeasurement;

$unitOfMeasurement->setCode(\Ups\Entity\UnitOfMeasurement::UOM_KM);

$unitOfMeasurement->setDescription('Kilometers');

$locatorRequest->setUnitOfMeasurement($unitOfMeasurement);

try {

// Get the locations

$locator = new Ups\Locator($accessKey, $userId, $password);

$locations = $locator->getLocations($locatorRequest, \Ups\Locator::OPTION_UPS_ACCESS_POINT_LOCATIONS);

foreach($locations->SearchResults->DropLocation as $location) {

// Your code here

var_dump($location);

}

} catch (Exception $e) {

var_dump($e);

}

9. 国际单 Tradeability Class

The Tradeability class allows you to get data for international shipments:

Landed Costs (e.g. duties)

Denied Party Screener

Import Compliance

Export License Detection

Note: only the Landed Costs API is currently implemented.

WARNING: Tradeability is only available through a SOAP API. Therefore you are required to have the SOAP extension installed on your system.

// Build request

$landedCostRequest = new \Ups\Entity\Tradeability\LandedCostRequest;

// Build shipment

$shipment = new \Ups\Entity\Tradeability\Shipment;

$shipment->setOriginCountryCode('NL');

$shipment->setDestinationCountryCode('US');

$shipment->setDestinationStateProvinceCode('TX');

$shipment->setResultCurrencyCode('EUR');

$shipment->setTariffCodeAlert(1);

$shipment->setTransportationMode(\Ups\Entity\Tradeability\Shipment::TRANSPORT_MODE_AIR);

$shipment->setTransactionReferenceId('1');

// Build product

$product = new \Ups\Entity\Tradeability\Product;

$product->setProductName('Test');

$tariffInfo = new \Ups\Entity\Tradeability\TariffInfo;

$tariffInfo->setTariffCode('5109.90.80.00');

$product->setTariffInfo($tariffInfo);

$product->setProductCountryCodeOfOrigin('BD');

$unitPrice = new \Ups\Entity\Tradeability\UnitPrice;

$unitPrice->setMonetaryValue(250);

$unitPrice->setCurrencyCode('EUR');

$product->setUnitPrice($unitPrice);

$weight = new Ups\Entity\Tradeability\Weight;

$weight->setValue(0.83);

$unitOfMeasurement = new \Ups\Entity\Tradeability\UnitOfMeasurement;

$unitOfMeasurement->setCode('kg');

$weight->setUnitOfMeasurement($unitOfMeasurement);

$product->setWeight($weight);

$quantity = new \Ups\Entity\Tradeability\Quantity;

$quantity->setValue(5);

$unitOfMeasurement = new \Ups\Entity\Tradeability\UnitOfMeasurement;

$unitOfMeasurement->setCode(\Ups\Entity\Tradeability\UnitOfMeasurement::PROD_PIECES);

$quantity->setUnitOfMeasurement($unitOfMeasurement);

$product->setQuantity($quantity);

$product->setTariffCodeAlert(1);

// Add product to shipment

$shipment->addProduct($product);

// Query request

$queryRequest = new \Ups\Entity\Tradeability\QueryRequest;

$queryRequest->setShipment($shipment);

$queryRequest->setSuppressQuestionIndicator(true);

// Build

$landedCostRequest->setQueryRequest($queryRequest);

try {

// Get the data

$api = new Ups\Tradeability($accessKey, $userId, $password);

$result = $api->getLandedCosts($landedCostRequest);

var_dump($result);

} catch (Exception $e) {

var_dump($e);

}

10 . Shipping Class

The Shipping class allows you to register shipments. This also includes return shipments.

The shipping flow consists of 2 steps:

Confirm: Send information to UPS to get it validated and get a digest you can use to accept the shipment.

Accept: Finalise the shipment, mark it as it will be shipped. Get label and additional information.

// Start shipment

$shipment = new Ups\Entity\Shipment;

// Set shipper

$shipper = $shipment->getShipper();

$shipper->setShipperNumber('XX');

$shipper->setName('XX');

$shipper->setAttentionName('XX');

$shipperAddress = $shipper->getAddress();

$shipperAddress->setAddressLine1('XX');

$shipperAddress->setPostalCode('XX');

$shipperAddress->setCity('XX');

$shipperAddress->setStateProvinceCode('XX'); // required in US

$shipperAddress->setCountryCode('XX');

$shipper->setAddress($shipperAddress);

$shipper->setEmailAddress('XX');

$shipper->setPhoneNumber('XX');

$shipment->setShipper($shipper);

// To address

$address = new \Ups\Entity\Address();

$address->setAddressLine1('XX');

$address->setPostalCode('XX');

$address->setCity('XX');

$address->setStateProvinceCode('XX'); // Required in US

$address->setCountryCode('XX');

$shipTo = new \Ups\Entity\ShipTo();

$shipTo->setAddress($address);

$shipTo->setCompanyName('XX');

$shipTo->setAttentionName('XX');

$shipTo->setEmailAddress('XX');

$shipTo->setPhoneNumber('XX');

$shipment->setShipTo($shipTo);

// From address

$address = new \Ups\Entity\Address();

$address->setAddressLine1('XX');

$address->setPostalCode('XX');

$address->setCity('XX');

$address->setStateProvinceCode('XX');

$address->setCountryCode('XX');

$shipFrom = new \Ups\Entity\ShipFrom();

$shipFrom->setAddress($address);

$shipFrom->setName('XX');

$shipFrom->setAttentionName($shipFrom->getName());

$shipFrom->setCompanyName($shipFrom->getName());

$shipFrom->setEmailAddress('XX');

$shipFrom->setPhoneNumber('XX');

$shipment->setShipFrom($shipFrom);

// Sold to

$address = new \Ups\Entity\Address();

$address->setAddressLine1('XX');

$address->setPostalCode('XX');

$address->setCity('XX');

$address->setCountryCode('XX');

$address->setStateProvinceCode('XX');

$soldTo = new \Ups\Entity\SoldTo;

$soldTo->setAddress($address);

$soldTo->setAttentionName('XX');

$soldTo->setCompanyName($soldTo->getAttentionName());

$soldTo->setEmailAddress('XX');

$soldTo->setPhoneNumber('XX');

$shipment->setSoldTo($soldTo);

// Set service

$service = new \Ups\Entity\Service;

$service->setCode(\Ups\Entity\Service::S_STANDARD);

$service->setDescription($service->getName());

$shipment->setService($service);

// Mark as a return (if return)

if ($return) {

$returnService = new \Ups\Entity\ReturnService;

$returnService->setCode(\Ups\Entity\ReturnService::PRINT_RETURN_LABEL_PRL);

$shipment->setReturnService($returnService);

}

// Set description

$shipment->setDescription('XX');

// Add Package

$package = new \Ups\Entity\Package();

$package->getPackagingType()->setCode(\Ups\Entity\PackagingType::PT_PACKAGE);

$package->getPackageWeight()->setWeight(10);

$unit = new \Ups\Entity\UnitOfMeasurement;

$unit->setCode(\Ups\Entity\UnitOfMeasurement::UOM_KGS);

$package->getPackageWeight()->setUnitOfMeasurement($unit);

// Set dimensions

$dimensions = new \Ups\Entity\Dimensions();

$dimensions->setHeight(50);

$dimensions->setWidth(50);

$dimensions->setLength(50);

$unit = new \Ups\Entity\UnitOfMeasurement;

$unit->setCode(\Ups\Entity\UnitOfMeasurement::UOM_CM);

$dimensions->setUnitOfMeasurement($unit);

$package->setDimensions($dimensions);

// Add descriptions because it is a package

$package->setDescription('XX');

// Add this package

$shipment->addPackage($package);

// Set Reference Number

$referenceNumber = new \Ups\Entity\ReferenceNumber;

if ($return) {

$referenceNumber->setCode(\Ups\Entity\ReferenceNumber::CODE_RETURN_AUTHORIZATION_NUMBER);

$referenceNumber->setValue($return_id);

} else {

$referenceNumber->setCode(\Ups\Entity\ReferenceNumber::CODE_INVOICE_NUMBER);

$referenceNumber->setValue($order_id);

}

$shipment->setReferenceNumber($referenceNumber);

// Set payment information

$shipment->setPaymentInformation(new \Ups\Entity\PaymentInformation('prepaid', (object)array('AccountNumber' => 'XX')));

// Ask for negotiated rates (optional)

$rateInformation = new \Ups\Entity\RateInformation;

$rateInformation->setNegotiatedRatesIndicator(1);

$shipment->setRateInformation($rateInformation);

// Get shipment info

try {

$api = new Ups\Shipping($accessKey, $userId, $password);

$confirm = $api->confirm(\Ups\Shipping::REQ_VALIDATE, $shipment);

var_dump($confirm); // Confirm holds the digest you need to accept the result

if ($confirm) {

$accept = $api->accept($confirm->ShipmentDigest);

var_dump($accept); // Accept holds the label and additional information

}

} catch (\Exception $e) {

var_dump($e);

}

If you wanted to create a printable file from the UPS Shipping label image data that came back with $accept, you would use something like the following:

$label_file = $order_id . ".gif";

$base64_string = $accept->PackageResults->LabelImage->GraphicImage;

$ifp = fopen($label_file, 'wb');

fwrite($ifp, base64_decode($base64_string));

fclose($ifp);

11. 报错日志 Logging

All constructors take a PSR-3 compatible logger.

Besides that, the main UPS class has a public method setLogger to set it after the constructor ran.

Requests & responses (including XML, no access keys) are logged at DEBUG level. At INFO level only the event is reported, not the XML content. More severe problems (e.g. no connection) are logged with higher severity.

Example using Monolog

// Create logger

$log = new \Monolog\Logger('ups');

$log->pushHandler(new \Monolog\Handler\StreamHandler('logs/ups.log', \Monolog\Logger::DEBUG));

// Create Rate object + insert logger

$rate = new Ups\Rate($key, $username, $password, $useIntegration, $log);

12. 个人的小小整合实例:

/**

* @filesource : ups.php

* @Author : justcode.ikeepstudying.com

* @copyright : Copyright (C) 2017-2018 GLS IT Studio NY

* @version : Created on Dec 6, 2017 10:12:19 AM

* @Github : https://github.com/gabrielbull/php-ups-api

*

*/

include_once rtrim(dirname(__FILE__), '/').'/vendor/autoload.php';

class UPS

{

private $ups_account;

private $ups_access_key;

private $ups_user_id;

private $ups_user_password;

private $shipment;

private $service;

private $package;

private $dimensions;

private $address_from;

private $address_to;

private $ship_from;

private $ship_to;

private $pickup;

private $request;

private $type;

private $services = array(

'01' => 'UPS Next Day Air',

'02' => 'UPS Second Day Air',

'03' => 'UPS Ground',

'07' => 'UPS Worldwide Express',

'08' => 'UPS Worldwide Expedited',

'11' => 'UPS Standard',

'12' => 'UPS Three-Day Select',

'13' => 'Next Day Air Saver',

'14' => 'UPS Next Day Air Early AM',

'54' => 'UPS Worldwide Express Plus',

'59' => 'UPS Second Day Air AM',

'65' => 'UPS Saver',

'70' => 'UPS Access Point Economy',

'93' => 'UPS Sure Post',

);

function __construct($param = array())

{

$this->ups_account = $param['ups_account'] ?? UPS_ACCOUNT;

$this->ups_access_key = $param['ups_access_key'] ?? UPS_ACCESS_KEY;

$this->ups_user_id = $param['ups_user_id'] ?? UPS_USER_ID;

$this->ups_user_password = $param['ups_user_password'] ?? UPS_USER_PASSWORD;

$this->request = new \Ups\Entity\TimeInTransitRequest;

$this->shipment = new \Ups\Entity\Shipment();

$this->ship_from = new \Ups\Entity\ShipFrom();

$this->ship_to = new \Ups\Entity\ShipTo();

$this->service = new \Ups\Entity\Service;

$this->package = new \Ups\Entity\Package();

$this->dimensions = new \Ups\Entity\Dimensions();

return $this;

}

public function __set($property, $value)

{

$this->{$property} = $value;

}

function shipper($param=array())

{

$shipper = $this->shipment->getShipper();

if(isset($param['number'])) $shipper->setShipperNumber($param['number']);

if(isset($param['att'])) $shipper->setAttentionName($param['att']);

if(isset($param['name'])) $shipper->setName($param['name']);

$shipperAddress = $shipper->getAddress();

$shipper->setAddress($shipperAddress);

$this->shipment->setShipper($shipper);

return $this;

}

function type($type = '')

{

$this->type = $type;

return $this;

}

function from($param=array(), $type = '')

{

$type = $type ? $type : $this->type;

switch ($type)

{

case 'artifact':

$address_from = new \Ups\Entity\AddressArtifactFormat;

if(isset($param['city'])) $address_from->setPoliticalDivision3($param['city']);

if(isset($param['zip'])) $address_from->setPostcodePrimaryLow($param['zip']);

if(isset($param['country'])) $address_from->setCountryCode($param['country']);

$this->request->setTransitFrom($address_from);

break;

default:

$address_from = new \Ups\Entity\Address();

if(isset($param['name'])) $address_from->setAttentionName($param['name']);

if(isset($param['address1'])) $address_from->setAddressLine1($param['address1']);

if(isset($param['address2'])) $address_from->setAddressLine2($param['address2']);

if(isset($param['address3'])) $address_from->setAddressLine3($param['address3']);

if(isset($param['state'])) $address_from->setStateProvinceCode($param['state']); // Required in US

if(isset($param['city'])) $address_from->setCity($param['city']);

if(isset($param['country'])) $address_from->setCountryCode($param['country']);

if(isset($param['zip'])) $address_from->setPostalCode($param['zip']);

$this->address_from = $address_from;

$this->ship_from->setAddress($address_from);

$this->shipment->setShipFrom($this->ship_from);

break;

}

return $this;

}

function to($param=array(), $type = '')

{

$type = $type ? $type : $this->type;

switch ($type)

{

case 'artifact':

$address_to = new \Ups\Entity\AddressArtifactFormat;

if(isset($param['city'])) $address_to->setPoliticalDivision3($param['city']);

if(isset($param['zip'])) $address_to->setPostcodePrimaryLow($param['zip']);

if(isset($param['country'])) $address_to->setCountryCode($param['country']);

$this->request->setTransitTo($address_to);

break;

default:

$address_to = new \Ups\Entity\Address();

if(isset($param['name'])) $address_to->setAttentionName($param['name']);

if(isset($param['address1'])) $address_to->setAddressLine1($param['address1']);

if(isset($param['address2'])) $address_to->setAddressLine2($param['address2']);

if(isset($param['address3'])) $address_to->setAddressLine3($param['address3']);

if(isset($param['state'])) $address_to->setStateProvinceCode($param['state']); // Required in US

if(isset($param['city'])) $address_to->setCity($param['city']);

if(isset($param['country'])) $address_to->setCountryCode($param['country']);

if(isset($param['zip'])) $address_to->setPostalCode($param['zip']);

$this->address_to = $address_to;

$this->ship_to->setAddress($address_to);

$this->shipment->setShipTo($this->ship_to);

break;

}

return $this;

}

/**

* ups/vendor/gabrielbull/ups-api/src/Entity/Service.php

$services = array(

'1' => S_AIR_1DAY,

'2' => S_AIR_2DAY,

'3' => S_GROUND,

'7' => S_WW_EXPRESS,

'8' => S_WW_EXPEDITED,

'11' => S_STANDARD,

'12' => S_3DAYSELECT,

'13' => S_AIR_1DAYSAVER,

'14' => S_AIR_1DAYEARLYAM,

'54' => S_WW_EXPRESSPLUS,

'59' => S_AIR_2DAYAM,

'65' => S_SAVER,

'70' => S_ACCESS_POINT,

'93' => S_SURE_POST,

);

*/

function service($class = '03')

{

if($class*1<10) $class = '0'.abs($class);

$this->service->setCode($class);

$this->service->setDescription($this->service->getName());

$this->shipment->setService($this->service);

return $this;

}

function weight($lbs, $type = '')

{

$type = $type ? $type : $this->type;

$unit = new \Ups\Entity\UnitOfMeasurement;

$unit->setCode(\Ups\Entity\UnitOfMeasurement::UOM_LBS);

switch ($type)

{

case 'artifact':

$shipmentWeight = new \Ups\Entity\ShipmentWeight;

$shipmentWeight->setWeight($lbs);

$shipmentWeight->setUnitOfMeasurement($unit);

$this->request->setShipmentWeight($shipmentWeight);

break;

default:

$this->package->getPackagingType()->setCode(\Ups\Entity\PackagingType::PT_PACKAGE);

$this->package->getPackageWeight()->setWeight($lbs);

$this->package->getPackageWeight()->setUnitOfMeasurement($unit);

break;

}

return $this;

}

function size($length = '', $width = '', $height = '')

{

if(is_array($length) AND $length)

{

$length = $length['length'] ?? $length;

$width = $length['width'] ?? $width;

$height = $length['height'] ?? $height;

}

$this->dimensions->setLength($length);

$this->dimensions->setWidth($width);

$this->dimensions->setHeight($height);

$unit = new \Ups\Entity\UnitOfMeasurement;

$unit->setCode(\Ups\Entity\UnitOfMeasurement::UOM_IN);

$this->dimensions->setUnitOfMeasurement($unit);

return $this;

}

function packages($number=1)

{

$this->request->setTotalPackagesInShipment($number);

return $this;

}

function invoice($amount, $currency = 'USD')

{

$invoiceLineTotal = new \Ups\Entity\InvoiceLineTotal;

$invoiceLineTotal->setMonetaryValue($amount);

$invoiceLineTotal->setCurrencyCode($currency);

$this->request->setInvoiceLineTotal($invoiceLineTotal);

return $this;

}

function pickup($date = '')

{

$this->pickup = date('Y-m-d', ($date ? strtotime(str_replace('-', '/', $date)) : time()));

return $this;

}

function tracking($tracking_number)

{

$tracking = new \Ups\Tracking($this->ups_access_key, $this->ups_user_id, $this->ups_user_password);

try {

$shipment = $tracking->track($tracking_number);

$response = $shipment->Package->Activity;

}

catch (Exception $e) { $response = $e->getMessage(); }

return $response;

}

function ratetime($zip_from, $zip_to)

{

$rate = new Ups\RateTimeInTransit($this->ups_access_key, $this->ups_user_id, $this->ups_user_password);

$address = new \Ups\Entity\Address();

$address->setPostalCode($zip_from);

$shipFrom = new \Ups\Entity\ShipFrom();

$shipFrom->setAddress($address);

$this->shipment->setShipFrom($shipFrom);

$shipTo = $this->shipment->getShipTo();

$shipToAddress = $shipTo->getAddress();

$shipToAddress->setPostalCode($zip_to);

$this->package->setDimensions($this->dimensions);

$this->shipment->addPackage($this->package);

$deliveryTimeInformation = new \Ups\Entity\DeliveryTimeInformation();

$deliveryTimeInformation->setPackageBillType(\Ups\Entity\DeliveryTimeInformation::PBT_NON_DOCUMENT);

$this->shipment->setDeliveryTimeInformation($deliveryTimeInformation);

try

{

$response = array();

$result = $rate->shopRatesTimeInTransit($this->shipment)->RatedShipment;

if($result) foreach ($result as $row)

{

$service_summary = $row->TimeInTransit->ServiceSummary;

$service_arrival = $service_summary->getEstimatedArrival();

$response[] = array(

'service' => $service_summary->Service->getDescription(),

'fee' => $row->TotalCharges->MonetaryValue,

'currency' => $row->TotalCharges->CurrencyCode,

'business' => $service_arrival->getBusinessDaysInTransit(),

'delivery' => date('Y-m-d H:i:s', strtotime($service_arrival->getArrival()->getDate().' '.$service_arrival->getArrival()->getTime())),

);

}

}

catch (Exception $e) { $response = $e->getMessage(); }

return $response;

}

function rate()

{

$rate = new \Ups\Rate($this->ups_access_key, $this->ups_user_id, $this->ups_user_password);

$this->shipment->addPackage($this->package);

try

{

$response = array();

$result = $rate->getRate($this->shipment)->RatedShipment;

if($result) foreach ($result as $row)

{

$response[] = array(

'service' => $this->services[$row->Service->getCode()] ?? '--',

'fee' => $row->TotalCharges->MonetaryValue,

'currency' => $row->TotalCharges->CurrencyCode,

'business' => $row->GuaranteedDaysToDelivery,

'delivery' => date('Y-m-d', strtotime('+'.$row->GuaranteedDaysToDelivery.' days')).' '.$row->ScheduledDeliveryTime,

);

}

$response = count($response) > 1 ? $response : $response[0];

}

catch (Exception $e) { $response = $e->getMessage(); }

return $response;

}

/**

* The TimeInTransit Class allow you to get all transit times using the UPS TimeInTransit API.

* /var/www/html/system/libraries/calculators/ups/vendor/gabrielbull/ups-api/src/Entity/EstimatedArrival.php

* you need to manually change private vars to public vars

*

* @return array|string

*/

function timein()

{

$timeInTransit = new \Ups\TimeInTransit($this->ups_access_key, $this->ups_user_id, $this->ups_user_password);

// Pickup date

$this->request->setPickupDate(new DateTime($this->pickup));

try

{

$response = array();

$result = $timeInTransit->getTimeInTransit($this->request)->ServiceSummary;

if($result) foreach ($result as $row)

{

$response[] = array(

'service' => $row->Service->getDescription(),

'pickup' => $row->EstimatedArrival->getPickupDate().' '.$row->EstimatedArrival->getPickupTime(),

'business' => $row->EstimatedArrival->getBusinessTransitDays(),

'delivery' => $row->EstimatedArrival->getDate().' '.$row->EstimatedArrival->getTime(),

);

}

}

catch (Exception $e) { $response = $e->getMessage(); }

return $response;

}

function isAddr($param = array())

{

$address = new \Ups\Entity\Address();

if(isset($param['address1'])) $address->setAddressLine1($param['address1']);

if(isset($param['address2'])) $address->setAddressLine2($param['address2']);

if(isset($param['address3'])) $address->setAddressLine3($param['address3']);

if(isset($param['city'])) $address->setCity($param['city']);

if(isset($param['state'])) $address->setStateProvinceCode($param['state']);

if(isset($param['country'])) $address->setCountryCode($param['country']);

if(isset($param['zip'])) $address->setPostalCode($param['zip']);

$xav = new \Ups\AddressValidation($this->ups_access_key, $this->ups_user_id, $this->ups_user_password);

$xav->activateReturnObjectOnValidate(); //This is optional

try { $response = $xav->validate($address, $requestOption = \Ups\AddressValidation::REQUEST_OPTION_ADDRESS_VALIDATION, $maxSuggestion = 15); }

catch (Exception $e) { $response = $e->getMessage(); }

return $response;

}

function __destruct()

{

unset($this->request);

unset($this->shipment);

unset($this->ship_from);

unset($this->ship_to);

unset($this->service);

unset($this->package);

unset($this->dimensions);

unset($this->pickup);

unset($this->type);

}

}

调用:

$from = array(

'name' => 'Lisha',

'address1' => '412 University St.',

'city' => 'SF',

'state' => 'CA',

'country' => 'US',

'zip' => '94134',

);

$to = array(

'name' => 'gideon',

'address1' => '100 John St.',

'city' => 'NY',

'state' => 'NY',

'country' => 'US',

'zip' => '10038',

);

$canada = array(

'address1' => "1010 EASY ST",

'city' => 'OTTAWA',

'state' => 'ONTARIO',

'country' => 'CA',

'zip' => 'K1A 0B1',

);

$uk = array(

'address1' => "10 Downing Street",

'city' => 'LONDON',

'state' => '',

'country' => 'UK',

'zip' => 'SW1A 2AA',

);

$eur = array(

'address1' => "10 Downing Street",

'city' => 'Amsterdam',

'state' => '',

'country' => 'NL',

'zip' => '1000AA',

);

$ups = new UPS()

// dump($ups->weight(10)->size(12, 35, 8)->ratetime(10001, 94134));

// dump($ups->isAddr($canada));

// dump($ups->from($from)->to($to)->service()->weight(10)->size(12, 35, 8)->rate());

// $type = 'artifact';

// dump($ups->type($type)->from($from)->to($eur)->weight(10, $type)->packages(2)->invoice(100)->timein());

// dump($ups->from($from, $type)->to($to, $type)->weight(10, $type)->packages(2)->invoice(100)->pickup('2018-5-10')->timein());

注意,你需要写入自己的 UPS_ACCOUNT, UPS_ACCESS_KEY, UPS_USER_ID, UPS_USER_PASSWORD 参数,方法见最上面。

相关

Related Posts

JAVA: 史上最全maven pom.xml详解

什么是pom? pom作为项目对象模型。通过xml表示maven项目,使用pom.xml来实现。主要描述了项目:包括配置文件;开发者需要遵循的规则,缺陷管理系统,组织和licenses,项目的url,项目的依赖性,以及其他所有的项目相关因素。 快速察看: xml 代码 4.0.0.....................…

MongoDB 教程七: MongoDB和PHP结合开发

(视频来源:php兄弟连) MongoDB 系列教程索引 : MongoDB 教程索引 (附有视频) Linux上安装 MongoDB PHP扩展 在终端上安装 你可以在linux中执行以下命令来安装MongoDB 的 PHP…

HTML5对比HTML4带来的新变化

HTML5入门 HTML5是HTML的最新版本,目标是更好地开发网络应用程序。 HTML5比HTML4拥有更简洁的语法并且提供了更多新的特性。 本文将对HTML5做一个大体的介绍。 HTML5能做什么? 1. 离线存储 HTML5可以让你的WEB应用程序离线也能运行,它提供了一个称作“应用程序缓存"的离线存储功能,因此即使当用户离线,浏览器仍然能够访问到它所需的文件。 这些文件可以是HTML,CSS,Javascript 或者其它任何网页运行所需要的文件。 2.拖拽功能 HTML5提供了可以用来设计交互应用程序的本地拖拽功能,通过这个特性你可以拖动任何元素然后把它放到你想放到地方。 3.地理位置定位技术 HTML5的地理位置定位API可以让你与所信任的网站分享你当前的位置信息,当然,隐私问题是这一特性所主要考虑的,W3.org声明:…

php计算运费价格,PHP: UPS API, UPS 运费计算, UPS接口相关推荐

  1. 微信小程序类似购物车列表效果,计算总共价格数量,加减商品计算,滑动删除

    先看下效果图: 一个小demo,记录下. html: <!--pages/mycollect/mycollect.wxml--> <!--最外层大盒子 --> <view ...

  2. 如何选择好公司和计算好价格

    学习完投资理论,当然就要把理论转化为实践,进入股票的实际操作阶段.由于我所接触的是一个系统的投资理论体系,为了复习,也为了练习,现在把第一步选择好公司,计算好价格的过程完整的记录下来,分享给大家,希望 ...

  3. 第4节 树形计算可转换债券价格

    第4节 树形计算可转换债券价格 4.1 简介 4.2 计算可转换债券价格算法 4.3 Python代码实现计算 4.4 计算示例 4.1 简介 可转换债券是一种由公司发行的债券.此种债券的持有者在债券 ...

  4. ups计算软件_什么是UPS?为什么要用UPS?

    01什么是UPS? UPS(Uninterruptible Power Supply),即不间断电源,是将蓄电池(多为铅酸免维护蓄电池)与主机相连接,通过主机逆变器等模块电路将直流电转换成市电(交流电 ...

  5. 不间断电源ups标准_什么是ups不间断电源,ups电源的重要性。

    .科华UPS概述: 1.什么是UPS:UPS - 全译Uninterruptible Power System (不间断电源)在市电断电等电网故障时,不间断地为用户设备提供纯净的(交流)电能的一种能量 ...

  6. 只为超越而来, 施耐德电气银河E系列UPS独步中小功率UPS市场

    首先要强调一点,数据中心建设仍然是刚需,而且比以往任何时候都重要,因为数据中心是企业数字化转型的重要依托和基础.数据中心的发展正呈现出"两极化"的趋势:一方面,随着云计算.大数据等 ...

  7. 服务器ups后备式好还是在线式好,后备式UPS电源和在线式UPS工作原理和优缺点

    后备式UPS电源和在线式UPS工作原理和优缺点.目前市场上的UPS不间断电源主要分为两大类:在线式UPS电源与后备式UPS电源.我们在负载小功率设备的时候,如果设备本身对电能的要求不是很高的话,一般情 ...

  8. ups属于计算机设备吗,ups电源属于什么设备?ups电源的分类

    ups电源属于什么设备?ups电源的分类 ups电源是一种用来提供电能的设备,它能够在工作中完成特殊的直流电和市电的相互转换,为计算机或计算机网络服务器提供相关不间断的稳定电能的电源.当市电正常工作时 ...

  9. 计算机中ups的作用,UPS电源是什么?UPS电源有什么作用?

    可能很多朋友不知道UPS电源是什么?因为在我们的日常生活中比较少见,其实UPS电源也就是不间断电源,功能类似于一个蓄电池.如果我们的电脑连接上了UPS电源,当遇到突然停电的情况,电脑就会自动切换到UP ...

  10. 教你UPS电池估算方法,UPS配置不再难

    在实际项目中或与厂商的交流中,常常遇到这样的情况,根据UPS的输出容量和所要求的后备时,需要快速.粗略地给出相关电池的配置.而UPS本身的计算方法非常复杂,一般人根本无法操作,所以掌握简单的方法或估算 ...

最新文章

  1. BufferedInputStream与BufferedOutputStream用法简介
  2. java中 抽象类+接口
  3. Git commit -m与-am
  4. nsoutlineview 搜索_阿里巴巴搜索推荐广告三位一体的在线服务体系
  5. 罗小黑用flash做的_中影星美好看罗小黑战记正式定档!
  6. Mysql学习总结(56)——MySQL用户管理和权限设置
  7. CrystalReports水晶报表开发中遇到的问题
  8. 向Richedit插入动态Gif的实现(关于QQ表情功能的制作)
  9. codesys工控机_CoDeSys
  10. Multifactor Explanations of Asset Pricing Anomalies学术翻译
  11. 【技能】Chrome扩展程序的使用
  12. 常用的logo设计技巧
  13. 笔记:关于Google Play这个应用市场app signing的坑
  14. HTML实现简单的网页设计。
  15. 企业级机械硬盘和消费级机械硬盘有什么区别?
  16. microLED应该会取代OLED的
  17. flickr 的全局主键生成方案
  18. 南大计算机跨专业考研,GitHub - weizhang27/NJU-CS-Kaoyan: 南京大学计算机考研相关问题...
  19. kafka端口号修改
  20. flex4使用itemrenderer后报Error #1009

热门文章

  1. Python输入若干整数求和
  2. 计算机编程的地位,学习编程的重要性
  3. SpringBoot请求第三方接口
  4. CF 346 B vectorpair s[100]
  5. 沪深300指数历史年分成分股名单数据集(2008-2019年)
  6. 2012-2013QS计算机专业世界大学排名
  7. CSDN日报20170602 ——《程序员、技术主管和架构师》
  8. 如何解决控件附件上传时超大附件无法上传的问题
  9. 运动世界校园显示服务器异常,运动世界校园跑完是异常成绩,计入有效成绩里吗?...
  10. Methodology写作之定性分析详解