UPS XML-RPC Interface 0.2

UPS XML-RPC Interface is a simple XML-RPC service for connecting to UPS for shipping estimates.
UPS XML-RPC Interface is a simple XML-RPC service for connecting to UPS for shipping estimates. UPS XML-RPC Interface is written in PHP, but can be accessed by any program that can communicate with XML-RPC.

UPS XML-RPC Interface is released under the GPL3.

This is a first stab at creating an easy-to-use, language neutral, interface for getting a shipping estimate from UPS. So far it seems to work perfectly for our needs but bugs are likely.

To install the UPS XML-RPC interface you'll need to compile Apache with CURL, expat, and XML-RPC. You'll need this on the server you're running the UPS XML-RPC interface on and, if different, also on the server your application is running on.

In your PHP application you'll need a function to make XML-RPC requests. I've provided an example below. Any programming language that can make XML-RPC requests can make requests of the UPS XML-RPC interface in a similar way.

---
function remoteRequest ( $server, $method, $params ) {
$req = xmlrpc_encode_request ( $method, $params );
$headers = array (
'Content-Type: text/xml',
'Content-Length: ' . strlen ( $req )
);
$ch = curl_init ( "http://$server/" );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, True );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, 'POST' );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $req );
curl_setopt ( $ch, CURLOPT_TIMEOUT, 600 );
set_time_limit ( 600 );
$resp = curl_exec ( $ch );
set_time_limit ( 30 );
curl_close ( $ch );
return xmlrpc_decode ( $resp );
}
---

The structure of the XML-RPC request is very similar to the XML requests documented in the 'UPS OnLine Tools Rates and Service Selection Developers Guide'.

$resp = remoteRequest ( 'ups', 'ratingServiceSelectionRequest', $upsParams );

From your PHP application you can connect to a server, 'ups', and run the function, 'ratingServiceSelectionRequest', with the options defined in an array, $upsParams. The result will be returned as the $resp array.

To define your array that you'll submit there are several common options. You'll need a UPS shipper number, access key, username, and password. You'll also need to know the shipper's postal code, state code, and country code and the destination postal code, state code, and country code.

You'll need to define one or more packages. Shipments of more than 150lbs will need to go by UPS Freight. Smaller packages can be shipped by UPS Ground but must be calculated separately.

Below is a sample package definition. You can define the dimensions of the package if you know them for a more accurate shipping estimate. One or more packages should be put into a $packages array.

---
$package = array (
'packagingType' => array (
'code' => '02' // Customer supplied packaging.
),
'dimensions' => array (
'unitOfMeasurement' => array (
'code' => 'IN'
),
'length' => 1,
'width' => 1,
'height' => 1
),
'packageWeight' => array (
'unitOfMeasurement' => array (
'code' => 'LBS'
),
'weight' => $weight
)
);
---

Below is an example of a full options array.

---
$upsParams = array (
'accessKey' => UPS_ACCESS_KEY,
'username' => UPS_USERNAME,
'password' => UPS_PASSWORD,
'request' => array (
'requestAction' => 'rate',
'requestOption' => 'Rate'
),
'pickupType' => array (
'code' => '03' // customer counter
),
'customerClassification' => array (
'code' => '04' // retail
),
'shipment' => array (
'service' => array (
'code' => $service // UPS Freight (308) or UPS Ground (03)
),
'shipper' => array (
'shipperNumber' => UPS_SHIP_NUMBER,
'address' => array (
'postalCode' => UPS_SHIPPER_POSTAL,
'stateProvinceCode' => UPS_SHIPPER_STATE,
'countryCode' => UPS_SHIPPER_COUNTRY
)
),
'shipTo' => array (
'address' => array (
'postalCode' => $postalCode,
'stateProvinceCode' => $state,
'countryCode' => $country
)
),
'packages' => $packages
)
);

---

You'll need to check your response array to see if your request succeeded. If so, you'll probably want to add up all the charges. Otherwise you'll want to respond to the error in some way. Sample code to do this is below.

---
if ( $resp['response']['responseStatusCode'] == 1 ) {
$shipping = 0.00;
foreach ( $resp['ratedShipments'] as $ratedShipment ) {
$shipping = $shipping + (float) $ratedShipment['totalCharges']['monetaryValue'];
}
} else {
// an error has occured. do something.
}
---

Now you have a good estimate of the shipping charges. Add your handling fee and you're done.

What's New in This Release:

· Bugfixes.

last updated on:
July 13th, 2008, 1:47 GMT
price:
FREE!
developed by:
Michael McGlothlin
homepage:
www.plumbersstock.com
license type:
GPL v3 
category:
ROOT \ Internet \ HTTP (WWW)

FREE!

In a hurry? Add it to your Download Basket!

user rating 15

2.9/5
 

0/5

Rate it!

Add your review! 2 USER REVIEWS SO FAR

SUBMIT