//this document is included within another php script.

$userid = "homer";
$pass = "duffbeer";  

//// license key from UPS for xml tools. Sign up for free!
$activity = "activity"; /// UPS activity code 
$shipweight ="10"; // in lbs.
$sZip= "10025"; // my old zipcode

* Minimum required variables:
* $shipweight
* $sZip
* $svccode
* ### UPS Shipping vars
*  01 Next Day Air
*  02 2nd Day Air
*  03 Ground
*  07 Worldwide Express
*  08 Worldwide Expedited
*  11 Standard
*  12 3-Day Select
*  13 Next Day Air Saver
*  14 Next Day Air Early AM
*  54 Worldwide Express Plus
*  59 2nd Day Air AM
*  65 Express Saver
*  go to their website and download the XML manual for further explanation of their codes.

///// The below following is the query string to be posted to the UPS XML server. ///// 

$ratereq = "<?xml version=\"1.0\"?><AccessRequest xml:lang=\"en-US\"><AccessLicenseNumber>$access_key</AccessLicenseNumber>
<?xml version=\"1.0\"?><RatingServiceSelectionRequest xml:lang=\"en-US\"><Request>
<TransactionReference><CustomerContext>Rating and Service</CustomerContext><XpciVersion>1.0001</XpciVersion>
<Description>Package</Description></PackagingType><Description>Rate Shopping</Description><PackageWeight>

// www.php.net for more info
$ch = curl_init(); /// initialize a cURL session
$res= curl_setopt ($ch, CURLOPT_URL,"https://www.ups.com/ups.app/xml/Rate"); 
 ///set the post-to url (do not include the ?query+string here!)
 curl_setopt ($ch, CURLOPT_HEADER, 0); /// Header control 
 curl_setopt($ch, CURLOPT_POST, 1);  /// tell it to make a POST, not a GET 
 curl_setopt($ch, CURLOPT_POSTFIELDS, "$ratereq");  /// put the query string here starting with "?" 
 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); /// This allows the output to be set into a variable $xyz 
 $xyz = curl_exec ($ch); /// execute the curl session and return the output to a variable $xyz 
 // in this case, $xyz will contain unparsed XML data.
 curl_close ($ch); /// close the curl session 

/*time to parse XML, using the simple php XML parser: requires three user-defined functions:
* startElement(), endElement, and characterData. 
* both startElement() and endElement() could be used to format XML output into HTML.
* since all we want is the data with chardata, we mostly ignore these functions.
function startElement($parser, $name, $attrs) {
	global $currentTag;
	$currentTag = $name;
}// end startElement

function endElement($parser, $name) {
	global $currentTag;
	// clear current tag variable
	$currentTag = "";
}//end end element

function characterData($parser, $data) {
	global $currentTag;
    if($currentTag=="MONETARYVALUE"){ //XML tags must be uppercase
	global $shipcost;
	  $shipcost = $data;
	// format the data
} //end charData

// initialize parser
$xml_parser = xml_parser_create();

// set callback functions
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");

// read and parse data
while ($data = $xyz){
	if (!xml_parse($xml_parser,$data,true)){ // don't print out any messy errors.


// clean up
// should give us the price. 
  echo "Shipping Cost is $ $shipcost

"; } else{ echo "For some reason, we cannot provide you with a current quote. Please call or email for additional shipping information.
You may continue placing your order."; } ? >