I use https://onlinetools.ups.com/api/rating/v1/shop
Returns several rates at the same time.
<?php
/**
* Requires libcurl
*/
$curl = curl_init();
//Receive package info from query
$Weight = $_POST['Weight'];
$ReceiverZip = $_POST['Zip'];
//Set Receiver country
$ReceiverCountry = "US";
//Set your info
$UPSID = "YOUR UPS ACCOUNT NUMBER";
$ShipperName = "YOUR NAME";
$ShipperCity = "YOUR CITY";
$ShipperState = "YOUR STATE ABBREVIATION";
$ShipperZip = "YOUR ZIP";
$ShipperCountry = "US";
$clientId = "YOUR API CLIENT ID";
$clientSecret = "YOUR API CLIENT SECRET";
// Step 1: access token
curl_setopt_array($curl, [
CURLOPT_HTTPHEADER => [
"Content-Type: application/x-www-form-urlencoded",
"x-merchant-id: ".$UPSID,
"Authorization: Basic " . base64_encode("$clientId:$clientSecret")
],
CURLOPT_POSTFIELDS => "grant_type=client_credentials",
CURLOPT_PORT => "",
CURLOPT_URL => "https://onlinetools.ups.com/security/v1/oauth/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "POST",
]);
$response0 = curl_exec($curl);
$error = curl_error($curl);
curl_close($curl);
if ($error) {
echo "cURL Error #:" . $error;
} else {
$tokenData = json_decode($response0);
$accessToken = $tokenData->access_token;
}
// Step 2: shipment data
$payload = array(
"RateRequest" => array(
"Request" => array(
"TransactionReference" => array(
"CustomerContext" => "CustomerContext"
)
),
"Shipment" => array(
"Shipper" => array(
"Name" => $ShipperName,
"ShipperNumber" => $UPSID,
"Address" => array(
"AddressLine" => array(
"ShipperAddressLine",
"ShipperAddressLine",
"ShipperAddressLine"
),
"City" => $ShipperCity,
"StateProvinceCode" => $ShipperState,
"PostalCode" => $ShipperZip,
"CountryCode" => $ShipperCountry
)
),
"ShipTo" => array(
"Name" => "ShipToName",
"Address" => array(
"AddressLine" => array(
"ShipToAddressLine",
"ShipToAddressLine",
"ShipToAddressLine"
),
"PostalCode" => $ReceiverZip,
"CountryCode" => $ReceiverCountry
)
),
"ShipFrom" => array(
"Name" => "ShipFromName",
"Address" => array(
"AddressLine" => array(
"ShipFromAddressLine",
"ShipFromAddressLine",
"ShipFromAddressLine"
),
"City" => $ShipperCity,
"StateProvinceCode" => $ShipperState,
"PostalCode" => $ShipperZip,
"CountryCode" => $ShipperCountry
)
),
"PaymentDetails" => array(
"ShipmentCharge" => array(
array(
"Type" => "01",
"BillShipper" => array(
"AccountNumber" => $UPSID
)
)
)
),
"NumOfPieces" => "1",
"Package" => array(
"PackagingType" => array(
"Code" => "02",
"Description" => "Packaging"
),
"PackageWeight" => array(
"UnitOfMeasurement" => array(
"Code" => "LBS",
"Description" => "Pounds"
),
"Weight" => $Weight
)
)
)
)
);
//Rate shop
curl_setopt_array($curl, [
CURLOPT_HTTPHEADER => [
"Authorization: Bearer " . $accessToken,
"transId: string",
"transactionSrc: testing"
],
CURLOPT_POSTFIELDS => json_encode($payload),
CURLOPT_PORT => "",
CURLOPT_URL => "https://onlinetools.ups.com/api/rating/v1/shop",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "POST",
]);
$response = curl_exec($curl);
$error = curl_error($curl);
curl_close($curl);
if ($error) {
echo "cURL Error #:" . $error;
} else {
$decodedResponse = json_decode($response, true); // true for associative array
// Example using associative array
if (isset($decodedResponse['RateResponse']['RatedShipment'])) {
foreach ($decodedResponse['RateResponse']['RatedShipment'] as $shipment) {
$serviceCode = $shipment['Service']['Code'];
$rate = $shipment['TotalCharges']['MonetaryValue'];
switch ($serviceCode) {
case "01":
$ups_cost01 = $rate;
break;
case "02":
$ups_cost02 = $rate;
break;
case "03":
$ups_cost = $rate;
break;
case "12":
$ups_cost12 = $rate;
break;
default:
break;
}
}
}
}
?>