Using Simple Rates¶
What are simple rates?¶
Although RocketShipIt can return very detailed information about a particular rate or carrier services it is often easier to have access to a simple array that holds only the total monetary value of the rates. This is exactly what getSimpleRates() does.
Using it¶
UPS
The following code will generate an array of all available UPS services to 90210:
<?php
$rate = new \RocketShipIt\Rate('UPS');
$rate->setParameter('toCode', '90210');
$rate->setParameter('weight', '5');
$rate->setParameter('residentialAddressIndicator', '0');
$response = $rate->getSimpleRates();
FedEx
The following code will generate an array of all available FedEx services to 59715:
<?php
$rate = new \RocketShipIt\Rate('FedEx');
$rate->setParameter('toCode', '59715');
$rate->setParameter('weight', '5');
$response = $rate->getSimpleRates();
USPS
The following code will generate an array of all available USPS services to 90210:
<?php
$rate = new \RocketShipIt\Rate('USPS');
$rate->setParameter('toCode', '90210');
$rate->setParameter('weight', '5');
$response = $rate->getSimpleRates();
Simple rates also work with multiple packages:
<?php
$rate = new \RocketShipIt\Rate('USPS');
$rate->setParameter('toCode','90210');
$rate->setParameter('service','ALL');
$package = new \RocketShipIt\Package('USPS');
$package->setParameter('weight', 1);
$package->setParameter('length', 8);
$package->setParameter('width', 2);
$package->setParameter('height', 2);
$rate->addPackageToShipment($package);
$package = new \RocketShipIt\Package('USPS');
$package->setParameter('weight', 1);
$package->setParameter('length', 8);
$package->setParameter('width', 2);
$package->setParameter('height', 2);
$rate->addPackageToShipment($package);
$response = $rate->getSimpleRates();
Stamps.com
Example:
<?php
$rate = new \RocketShipIt\Rate('stamps');
$rate->setParameter('toCode', '90210');
$rate->setParameter('weight', '5');
$response = $rate->getSimpleRates();
Canada Post
Example:
<?php
$rate = new \RocketShipIt\Rate('canada');
$rate->setParameter('shipCity','BELLEVILLE');
$rate->setParameter('shipCode','K8N5W6');
$rate->setParameter('toCode','94115');
$rate->setParameter('weight','5');
$response = $rate->getSimpleRates();
Example UPS output¶
getSimpleRates() will produce an Array similar to this:
Array
(
[0] => Array
(
[desc] => UPS Ground
[rate] => 24.84
[service_code] => 03
)
[1] => Array
(
[desc] => UPS 3 Day Select
[rate] => 42.92
[service_code] => 12
)
[2] => Array
(
[desc] => UPS 2nd Day Air
[rate] => 58.10
[service_code] => 02
)
[3] => Array
(
[desc] => UPS Next Day Air Saver
[rate] => 139.26
[service_code] => 13
)
[4] => Array
(
[desc] => UPS Next Day Air Early AM
[rate] => 216.32
[service_code] => 14
)
[5] => Array
(
[desc] => UPS Next Day Air
[rate] => 149.42
[service_code] => 01
)
)
What happens when there is an error with the rate?:
Array
(
[error] => The requested service is unavailable between the selected locations.
)
RocketShipIt will still produce an array with a special rate called “error”. The value of this will be set to the english description of the error.
How do I detect an error?
There are many ways but the easiest is to use PHP’s array_key_exists():
<?php
if (array_key_exists('error', $response)) {
$theError = $response['error'];
}
Example FedEx output¶
getSimpleRates() will produce an Array similar to this:
Array
(
[0] => Array
(
[desc] => FedEx Priority Overnight
[rate] => 37.4
[service_code] => PRIORITY_OVERNIGHT
)
[1] => Array
(
[desc] => FedEx Standard Overnight
[rate] => 27.56
[service_code] => STANDARD_OVERNIGHT
)
[2] => Array
(
[desc] => FedEx 2 Day AM
[rate] => 17.78
[service_code] => FEDEX_2_DAY_AM
)
[3] => Array
(
[desc] => FedEx 2 Day
[rate] => 15.46
[service_code] => FEDEX_2_DAY
)
[4] => Array
(
[desc] => FedEx Express Saver
[rate] => 14.41
[service_code] => FEDEX_EXPRESS_SAVER
)
[5] => Array
(
[desc] => FedEx Ground
[rate] => 7.22
[service_code] => FEDEX_GROUND
)
)
Example USPS output¶
getSimpleRates() will produce an Array similar to this:
Array
(
[0] => Array
(
[desc] => Express Mail
[rate] => 54.65
[service_code] => 3
)
[1] => Array
(
[desc] => Express Mail Hold For Pickup
[rate] => 54.65
[service_code] => 2
)
[2] => Array
(
[desc] => Express Mail Flat Rate Boxes
[rate] => 39.95
[service_code] => 55
)
[3] => Array
(
[desc] => Express Mail Flat Rate Boxes Hold For Pickup
[rate] => 39.95
[service_code] => 56
)
[4] => Array
(
[desc] => Express Mail Flat Rate Envelope
[rate] => 19.95
[service_code] => 13
)
[5] => Array
(
[desc] => Express Mail Flat Rate Envelope Hold For Pickup
[rate] => 19.95
[service_code] => 27
)
[6] => Array
(
[desc] => Express Mail Legal Flat Rate Envelope
[rate] => 19.95
[service_code] => 30
)
[7] => Array
(
[desc] => Express Mail Legal Flat Rate Envelope Hold For Pickup
[rate] => 19.95
[service_code] => 31
)
[8] => Array
(
[desc] => Express Mail Padded Flat Rate Envelope
[rate] => 19.95
[service_code] => 62
)
[9] => Array
(
[desc] => Express Mail Padded Flat Rate Envelope Hold For Pickup
[rate] => 19.95
[service_code] => 63
)
[10] => Array
(
[desc] => Priority Mail
[rate] => 17.75
[service_code] => 1
)
[11] => Array
(
[desc] => Priority Mail Large Flat Rate Box
[rate] => 16.85
[service_code] => 22
)
[12] => Array
(
[desc] => Priority Mail Medium Flat Rate Box
[rate] => 12.35
[service_code] => 17
)
[13] => Array
(
[desc] => Priority Mail Small Flat Rate Box
[rate] => 5.80
[service_code] => 28
)
[14] => Array
(
[desc] => Priority Mail Flat Rate Envelope
[rate] => 5.60
[service_code] => 16
)
[15] => Array
(
[desc] => Priority Mail Legal Flat Rate Envelope
[rate] => 5.75
[service_code] => 44
)
[16] => Array
(
[desc] => Priority Mail Padded Flat Rate Envelope
[rate] => 5.95
[service_code] => 29
)
[17] => Array
(
[desc] => Priority Mail Gift Card Flat Rate Envelope
[rate] => 5.60
[service_code] => 38
)
[18] => Array
(
[desc] => Priority Mail Small Flat Rate Envelope
[rate] => 5.60
[service_code] => 42
)
[19] => Array
(
[desc] => Priority Mail Window Flat Rate Envelope
[rate] => 5.60
[service_code] => 40
)
[20] => Array
(
[desc] => Standard Post
[rate] => 13.12
[service_code] => 4
)
[21] => Array
(
[desc] => Media Mail
[rate] => 4.78
[service_code] => 6
)
[22] => Array
(
[desc] => Library Mail
[rate] => 4.55
[service_code] => 7
)
)
Example Stamps.com output¶
getSimpleRates() will produce an Array similar to this:
Array
(
[0] => Array
(
[desc] => USPS First Class Mail - Postcard
[rate] => 0.35
[service_code] => US-FC
[package_type] => Postcard
)
[1] => Array
(
[desc] => USPS Priority Mail - Letter
[rate] => 11.26
[service_code] => US-PM
[package_type] => Letter
)
[2] => Array
(
[desc] => USPS Priority Mail - Large Envelope or Flat
[rate] => 11.26
[service_code] => US-PM
[package_type] => Large Envelope or Flat
)
[3] => Array
(
[desc] => USPS Priority Mail - Thick Envelope
[rate] => 11.26
[service_code] => US-PM
[package_type] => Thick Envelope
)
[4] => Array
(
[desc] => USPS Priority Mail - Package
[rate] => 11.26
[service_code] => US-PM
[package_type] => Package
)
[5] => Array
(
[desc] => USPS Priority Mail - Flat Rate Box
[rate] => 11.95
[service_code] => US-PM
[package_type] => Flat Rate Box
)
[6] => Array
(
[desc] => USPS Priority Mail - Small Flat Rate Box
[rate] => 6.1
[service_code] => US-PM
[package_type] => Small Flat Rate Box
)
[7] => Array
(
[desc] => USPS Priority Mail - Large Flat Rate Box
[rate] => 16.35
[service_code] => US-PM
[package_type] => Large Flat Rate Box
)
[8] => Array
(
[desc] => USPS Priority Mail - Flat Rate Envelope
[rate] => 5.75
[service_code] => US-PM
[package_type] => Flat Rate Envelope
)
[9] => Array
(
[desc] => USPS Priority Mail - Flat Rate Padded Envelope
[rate] => 6.1
[service_code] => US-PM
[package_type] => Flat Rate Padded Envelope
)
[10] => Array
(
[desc] => USPS Priority Mail - Large Package
[rate] => 11.26
[service_code] => US-PM
[package_type] => Large Package
)
[11] => Array
(
[desc] => USPS Priority Mail - Regional Rate Box A
[rate] => 8.15
[service_code] => US-PM
[package_type] => Regional Rate Box A
)
[12] => Array
(
[desc] => USPS Priority Mail - Regional Rate Box B
[rate] => 10.66
[service_code] => US-PM
[package_type] => Regional Rate Box B
)
[13] => Array
(
[desc] => USPS Priority Mail - Legal Flat Rate Envelope
[rate] => 5.75
[service_code] => US-PM
[package_type] => Legal Flat Rate Envelope
)
[14] => Array
(
[desc] => USPS Express Mail - Letter
[rate] => 44.1
[service_code] => US-XM
[package_type] => Letter
)
[15] => Array
(
[desc] => USPS Express Mail - Large Envelope or Flat
[rate] => 44.1
[service_code] => US-XM
[package_type] => Large Envelope or Flat
)
[16] => Array
(
[desc] => USPS Express Mail - Thick Envelope
[rate] => 44.1
[service_code] => US-XM
[package_type] => Thick Envelope
)
[17] => Array
(
[desc] => USPS Express Mail - Package
[rate] => 44.1
[service_code] => US-XM
[package_type] => Package
)
[18] => Array
(
[desc] => USPS Express Mail - Flat Rate Envelope
[rate] => 20.66
[service_code] => US-XM
[package_type] => Flat Rate Envelope
)
[19] => Array
(
[desc] => USPS Express Mail - Flat Rate Padded Envelope
[rate] => 20.66
[service_code] => US-XM
[package_type] => Flat Rate Padded Envelope
)
[20] => Array
(
[desc] => USPS Express Mail - Large Package
[rate] => 44.1
[service_code] => US-XM
[package_type] => Large Package
)
[21] => Array
(
[desc] => USPS Express Mail - Legal Flat Rate Envelope
[rate] => 20.66
[service_code] => US-XM
[package_type] => Legal Flat Rate Envelope
)
[22] => Array
(
[desc] => USPS Media Mail - Large Envelope or Flat
[rate] => 4.72
[service_code] => US-MM
[package_type] => Large Envelope or Flat
)
[23] => Array
(
[desc] => USPS Media Mail - Thick Envelope
[rate] => 4.72
[service_code] => US-MM
[package_type] => Thick Envelope
)
[24] => Array
(
[desc] => USPS Media Mail - Package
[rate] => 4.72
[service_code] => US-MM
[package_type] => Package
)
[25] => Array
(
[desc] => USPS Media Mail - Large Package
[rate] => 4.72
[service_code] => US-MM
[package_type] => Large Package
)
[26] => Array
(
[desc] => USPS Parcel Select - Thick Envelope
[rate] => 11.21
[service_code] => US-PS
[package_type] => Thick Envelope
)
[27] => Array
(
[desc] => USPS Parcel Select - Package
[rate] => 11.21
[service_code] => US-PS
[package_type] => Package
)
[28] => Array
(
[desc] => USPS Parcel Select - Large Package
[rate] => 21.23
[service_code] => US-PS
[package_type] => Large Package
)
[29] => Array
(
[desc] => USPS Parcel Select - Oversized Package
[rate] => 105.11
[service_code] => US-PS
[package_type] => Oversized Package
)
[30] => Array
(
[desc] => USPS Library Mail - Large Envelope or Flat
[rate] => 4.51
[service_code] => US-LM
[package_type] => Large Envelope or Flat
)
[31] => Array
(
[desc] => USPS Library Mail - Thick Envelope
[rate] => 4.51
[service_code] => US-LM
[package_type] => Thick Envelope
)
[32] => Array
(
[desc] => USPS Library Mail - Package
[rate] => 4.51
[service_code] => US-LM
[package_type] => Package
)
)
Example Canada Post output¶
getSimpleRates() will produce an Array similar to this:
Array
(
[0] => Array
(
[desc] => Canada Post Expedited Parcel USA
[rate] => 36.18
[service_code] => USA.EP
)
[1] => Array
(
[desc] => Canada Post Priority Worldwide parcel USA
[rate] => 171.81
[service_code] => USA.PW.PARCEL
)
[2] => Array
(
[desc] => Canada Post Xpresspost USA
[rate] => 58.11
[service_code] => USA.XP
)
)