Calculate Shipping Rates using UPS API with cURL and PHP
January 24, 2024
$UPS_CustomerContext = '';
$UPS_shipper_Name = "Your Company";
$UPS_shipper_Number = "y886655";
$UPS_shipper_AddressLine_1 = "3355 Business Rd.";
$UPS_shipper_AddressLine_2 = "Ste A #66";
$UPS_shipper_AddressLine_3 = "";
$UPS_shipper_City = "New York";
$UPS_shipper_StateProvinceCode = "NY";
$UPS_shipper_PostalCode = "50098";
$UPS_shipper_CountryCode = "US";
$UPS_ship_from_Name = "Your Company";
$UPS_ship_from_AddressLine_1 = "3355 Business Rd.";
$UPS_ship_from_AddressLine_2 = "Ste A #66";
$UPS_ship_from_AddressLine_3 = "";
$UPS_ship_from_City = "New York";
$UPS_ship_from_StateProvinceCode = "NY";
$UPS_ship_from_PostalCode = "50098";
$UPS_ship_from_CountryCode = "US";
$UPS_ship_to_Name = "Peter Weiller";
$UPS_ship_to_ShipToAddressLine_1 = "2700 Cahuenga Blvd";
$UPS_ship_to_ShipToAddressLine_2 = "#4122";
$UPS_ship_to_ShipToAddressLine_3 = "";
$UPS_ship_to_City = "Los Angeles";
$UPS_ship_to_StateProvinceCode = "CA";
$UPS_ship_to_PostalCode = "90068";
$UPS_ship_to_CountryCode = "US";
$UPS_total_shipping_weight_lbs = 0;
$UPS_total_shipping_pieces = 0;
$UPS_package_Length = 0;
$UPS_package_Width = 0;
$UPS_package_Height = 0;
$UPS_package_Weight = 0;
// create the array
$UPS_shipping_array["RateRequest"]["Request"]["TransactionReference"]["CustomerContext"] = $UPS_CustomerContext;
$UPS_shipping_array["RateRequest"]["Shipment"]["Shipper"]["Name"] = $UPS_shipper_Name;
$UPS_shipping_array["RateRequest"]["Shipment"]["Shipper"]["ShipperNumber"] = $UPS_shipper_Number;
$UPS_shipping_array["RateRequest"]["Shipment"]["Shipper"]["Address"]["AddressLine"] = array(
$UPS_shipper_AddressLine_1,
$UPS_shipper_AddressLine_2,
$UPS_shipper_AddressLine_3
);
$UPS_shipping_array["RateRequest"]["Shipment"]["Shipper"]["Address"]["City"] = $UPS_shipper_City;
$UPS_shipping_array["RateRequest"]["Shipment"]["Shipper"]["Address"]["StateProvinceCode"] = $UPS_shipper_StateProvinceCode;
$UPS_shipping_array["RateRequest"]["Shipment"]["Shipper"]["Address"]["PostalCode"] = $UPS_shipper_PostalCode;
$UPS_shipping_array["RateRequest"]["Shipment"]["Shipper"]["Address"]["CountryCode"] = $UPS_shipper_CountryCode;
$UPS_shipping_array["RateRequest"]["Shipment"]["ShipTo"]["Name"] = $UPS_ship_to_Name;
$UPS_shipping_array["RateRequest"]["Shipment"]["ShipTo"]["Address"]["AddressLine"] = array(
$UPS_ship_to_ShipToAddressLine_1,
$UPS_ship_to_ShipToAddressLine_2,
$UPS_ship_to_ShipToAddressLine_3
);
$UPS_shipping_array["RateRequest"]["Shipment"]["ShipTo"]["Address"]["City"] = $UPS_ship_to_City;
$UPS_shipping_array["RateRequest"]["Shipment"]["ShipTo"]["Address"]["StateProvinceCode"] = $UPS_ship_to_StateProvinceCode;
$UPS_shipping_array["RateRequest"]["Shipment"]["ShipTo"]["Address"]["PostalCode"] = $UPS_ship_to_PostalCode;
$UPS_shipping_array["RateRequest"]["Shipment"]["ShipTo"]["Address"]["CountryCode"] = $UPS_ship_to_CountryCode;
$UPS_shipping_array["RateRequest"]["Shipment"]["ShipFrom"]["Name"] = $UPS_ship_from_Name;
$UPS_shipping_array["RateRequest"]["Shipment"]["ShipFrom"]["Address"]["Addressline"] = array(
$UPS_ship_from_AddressLine_1,
$UPS_ship_from_AddressLine_2,
$UPS_ship_from_AddressLine_3
);
$UPS_shipping_array["RateRequest"]["Shipment"]["ShipFrom"]["Address"]["City"] = $UPS_ship_from_City;
$UPS_shipping_array["RateRequest"]["Shipment"]["ShipFrom"]["Address"]["StateProvinceCode"] = $UPS_ship_from_StateProvinceCode;
$UPS_shipping_array["RateRequest"]["Shipment"]["ShipFrom"]["Address"]["PostalCode"] = $UPS_ship_from_PostalCode;
$UPS_shipping_array["RateRequest"]["Shipment"]["ShipFrom"]["Address"]["CountryCode"] = $UPS_ship_from_CountryCode;
$UPS_shipping_array["RateRequest"]["Shipment"]["PaymentDetails"]["ShipmentCharge"]["Type"] = "01";
$UPS_shipping_array["RateRequest"]["Shipment"]["PaymentDetails"]["ShipmentCharge"]["BillShipper"]["AccountNumber"] = $UPS_shipper_Number;
$UPS_shipping_array["RateRequest"]["Shipment"]["Service"]["Code"] = "03";
$UPS_shipping_array["RateRequest"]["Shipment"]["Service"]["Description"] = "Ground";
$UPS_shipping_array["RateRequest"]["Shipment"]["ShipmentTotalWeight"]["UnitOfMeasurement"]["Code"] = "LBS";
$UPS_shipping_array["RateRequest"]["Shipment"]["ShipmentTotalWeight"]["UnitOfMeasurement"]["Description"] = "Pounds";
$UPS_shipping_array["RateRequest"]["Shipment"]["ShipmentTotalWeight"]["Weight"] = $UPS_total_shipping_weight_lbs;
$UPS_shipping_array["RateRequest"]["Shipment"]["NumOfPieces"] = $UPS_total_shipping_pieces;
// add the three packages to the end of this array
$UPS_shipping_array["RateRequest"]["Shipment"]['Package'] = array();
// package 1
$packaging[1]["Length"] = "20";
$packaging[1]["Width"] = "5";
$packaging[1]["Height"] = "5";
$packaging[1]["Weight"] = "70";
// package 2
$packaging[2]["Length"] = "25";
$packaging[2]["Width"] = "7";
$packaging[2]["Height"] = "7";
$packaging[2]["Weight"] = "75";
// package 3
$packaging[3]["Length"] = "30";
$packaging[3]["Width"] = "9";
$packaging[3]["Height"] = "9";
$packaging[3]["Weight"] = "80";
foreach ($packaging as $a) {
$UPS_package_Length = $a["Length"];
$UPS_package_Width = $a["Width"];
$UPS_package_Height = $a["Height"];
$UPS_package_Weight = $a["Weight"];
$pkg = array();
$pkg['PackagingType'] = array(
'Code' => '02',
'Description' => 'Packaging'
);
$pkg['Dimensions'] = array(
'UnitOfMeasurement' => array('Code' => 'IN', 'Description' => 'Inches'),
'Length' => $UPS_package_Length,
'Width' => $UPS_package_Width,
'Height' => $UPS_package_Height
);
$pkg['PackageWeight'] = array(
'UnitOfMeasurement' => array('Code' => 'LBS', 'Description' => 'Pounds'),
'Weight' => $UPS_package_Weight
);
array_push($UPS_shipping_array["RateRequest"]["Shipment"]['Package'], $pkg);
$UPS_total_shipping_weight_lbs = $UPS_total_shipping_weight_lbs + $UPS_package_Weight;
$UPS_total_shipping_pieces ++ ;
}
/*
requestoption is required
Valid Values:
Rate = The server rates (The default Request option is Rate if a Request Option is not provided).
Shop = The server validates the shipment, and returns rates for all UPS products from the ShipFrom to the ShipTo addresses.
Rate is the only valid request option for Ground Freight Pricing requests. Length 10
*/
$requestoption = "Shop";
$url = "https://onlinetools.ups.com/api/rating/v1/" . $requestoption . "?";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($UPS_shipping_array));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl, CURLOPT_HTTPHEADER, [
"Authorization: Bearer $access_token",
"Content-Type: application/json",
"transId: $session_id"
]);
$curl_response = curl_exec($curl);
$curl_error = curl_error($curl);
$curl_errno = curl_errno($curl);
curl_close($curl);
if($curl_error){
echo 'Curl error: ' . $curl_error . ' Curl error number: ' . $curl_errno;
}else{
$data = json_decode($curl_response, true);
if(isset($data["RateResponse"]["Response"]["ResponseStatus"]["Code"])){
$ResponseStatus_Code = $data["RateResponse"]["Response"]["ResponseStatus"]["Code"];
}
if(isset($data["RateResponse"]["Response"]["ResponseStatus"]["Description"])){
$ResponseStatus_Description = $data["RateResponse"]["Response"]["ResponseStatus"]["Description"];
}
// check for these errors... if you want.
/*
$data[RateResponse][Response][Alert] => Array
(
[0] => Array
(
[Code] => 119001
[Description] => Additional Handling has automatically been set on Package 1.
)
[1] => Array
(
[Code] => 110971
[Description] => Your invoice may vary from the displayed reference rates
)
[2] => Array
(
[Code] => 110920
[Description] => Ship To Address Classification is changed from Commercial to Residential
)
)
*/
if(isset($data["RateResponse"]["Response"]["TransactionReference"]["CustomerContext"])){
$CustomerContext = $data["RateResponse"]["Response"]["TransactionReference"]["CustomerContext"];
}
if(isset($data["RateResponse"]["Response"]["TransactionReference"]["TransactionIdentifier"])){
$TransactionIdentifier = $data["RateResponse"]["Response"]["TransactionReference"]["TransactionIdentifier"];
}
if(isset($data["RateResponse"]["RatedShipment"])){
$RatedShipment = $data["RateResponse"]["RatedShipment"];
}
foreach($RatedShipment as $RatedShipment_k => $RatedShipment_v){
if(isset($RatedShipment_v["BillingWeight"]["UnitOfMeasurement"]["Code"])){
$UnitOfMeasurement_Code = $RatedShipment_v["BillingWeight"]["UnitOfMeasurement"]["Code"];
}
if(isset($RatedShipment_v["BillingWeight"]["UnitOfMeasurement"]["Description"])){
$UnitOfMeasurement_Description = $RatedShipment_v["BillingWeight"]["UnitOfMeasurement"]["Description"];
}
if(isset($RatedShipment_v["BillingWeight"]["Weight"])){
$BillingWeight = $RatedShipment_v["BillingWeight"]["Weight"];
}
if(isset($RatedShipment_v["TransportationCharges"]["MonetaryValue"])){
$TransportationCharges = $RatedShipment_v["TransportationCharges"]["MonetaryValue"];
}
if(isset($RatedShipment_v["ServiceOptionsCharges"]["MonetaryValue"])){
$ServiceOptionsCharges = $RatedShipment_v["ServiceOptionsCharges"]["MonetaryValue"];
}
if(isset($RatedShipment_v["TotalCharges"]["MonetaryValue"])){
$TotalCharges = $RatedShipment_v["TotalCharges"]["MonetaryValue"];
}
if(isset($RatedShipment_v["GuaranteedDelivery"]["BusinessDaysInTransit"])){
$GuaranteedDelivery_BusinessDaysInTransit = $RatedShipment_v["GuaranteedDelivery"]["BusinessDaysInTransit"];
}
if(isset($RatedShipment_v["GuaranteedDelivery"]["DeliveryByTime"])){
$GuaranteedDelivery_DeliveryByTime = $RatedShipment_v["GuaranteedDelivery"]["DeliveryByTime"];
}
echo 'Total Weight: '.$BillingWeight.' '.$UnitOfMeasurement_Description.'< br >';
echo 'Transportation Charges: '.$TransportationCharges.'< br >';
echo 'Service Options Charges: '.$ServiceOptionsCharges.'< br >';
echo 'Total Charges: '.$TotalCharges.'< br >';
echo 'Guaranteed Delivery - Business Days: '.$GuaranteedDelivery_BusinessDaysInTransit.'< br >';
echo 'Guaranteed Delivery - Delivery By Time: '.$GuaranteedDelivery_DeliveryByTime.'< br >';
echo '< br > < br >';
if(isset($RatedShipment_v["RatedPackage"])){
$RatedPackages = $RatedShipment_v["RatedPackage"];
}
foreach($RatedPackages as $RatedPackages_k => $RatedPackages_v){
if(isset($RatedPackages_v["TransportationCharges"]["MonetaryValue"])){
$TransportationCharges = $RatedPackages_v["TransportationCharges"]["MonetaryValue"];
}
if(isset($RatedPackages_v["ServiceOptionsCharges"]["MonetaryValue"])){
$ServiceOptionsCharges = $RatedPackages_v["ServiceOptionsCharges"]["MonetaryValue"];
}
if(isset($RatedPackages_v["TotalCharges"]["MonetaryValue"])){
$TotalCharges = $RatedPackages_v["TotalCharges"]["MonetaryValue"];
}
if(isset($RatedPackages_v["Weight"])){
$Weight = $RatedPackages_v["Weight"];
}
if(isset($RatedPackages_v["BillingWeight"]["Weight"])){
$BillingWeight = $RatedPackages_v["BillingWeight"]["Weight"];
}
if(isset($RatedPackages_v["BillingWeight"]["UnitOfMeasurement"]["Description"])){
$BillingWeight_UnitOfMeasurement = $RatedPackages_v["BillingWeight"]["UnitOfMeasurement"]["Description"];
}
echo 'Weight: '.$Weight.' '.$BillingWeight_UnitOfMeasurement.'< br >';
echo 'Billing Weight: '.$BillingWeight.' '.$BillingWeight_UnitOfMeasurement.'< br >';
echo 'Transportation Charges: '.$TransportationCharges.'< br >';
echo 'Service Options Charges: '.$ServiceOptionsCharges.'< br >';
echo 'Total Charges: '.$TotalCharges.'< br >';
echo '< br >';
}
}
}