This library is aimed at wrapping all the UPS API into a simple to use PHP Library. It currently covers the Quantum View®, Tracking API, Shipping API, Rating API and Time in Transit API. Feel free to contribute.
- Requirements
- Installation
- Address Validation Class
- QuantumView Class
- Tracking Class
- Rate Class
- TimeInTransit Class
- Shipping Class
- Logging
This library uses PHP 5.3+.
To use the UPS API, you have to request an access key from UPS. For every request, you will have to provide the Access Key, your UPS User ID and Password.
It is recommended that you install the PHP UPS API Wrapper library through composer. To do so,
add the following lines to your composer.json file.
{
"require": {
"gabrielbull/ups-api": "dev-master"
}
}The Address Validation Class allow you to validate an address at street level. Suggestions are given when address is invalid.
Note: UPS has two Address Validations. This is Street Level option, which includes all option of the normal Address Validation class and adds street level validation.
Not all countries are supported, see UPS documentation. Currently US & Puerto Rico are supported.
$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);
try {
$response = $xav->validate($address, $requestOption = \Ups\AddressValidation::REQUEST_OPTION_ADDRESS_VALIDATION, $maxSuggestion = 15);
} catch (Exception $e) {
var_dump($e);
}Adress Validation parameters are:
addressAddress object as constructed in examplerequestOptionOne of the three request options. See documentation. Default = Address Validation.maxSuggestionMaximum number of suggestions to be returned. Max = 50
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);
}QuantumView parameters are:
nameName of subscription requested by user. If null, all events will be returned.beginDateTimeBeginning date time for the retrieval criteria of the subscriptions. Format: Y-m-d H:i:s or Unix timestamp.endDateTimeEnding date time for the retrieval criteria of the subscriptions. Format: Y-m-d H:i:s or Unix timestamp.fileNameFile name of specific subscription requested by user.bookmarkBookmarks the file for next retrieval.
If you provide a beginDateTime, but no endDateTime, the endDateTime will default to the current date time.
To use the fileName parameter, do not provide a beginDateTime.
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);
}Tracking parameters are:
trackingNumberThe package’s tracking number.requestOptionOptional processing. For Mail Innovations the only valid options are Last Activity and All activity.
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);
$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);
}rateRequestMandatory. rateRequest Object with shipment details
This Rate class is not finished yet! Parameter should be added when it will be finished.
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);
}timeInTransitRequestMandatory. timeInTransitRequest Object with shipment details, see example above.
Documentation for this class is coming.
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);