<?php
//this document is included within another php script.

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

$access_key = "XXXXXXXXXXXXXXXXXXXXXX";  
//// 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>
<UserId>$userid</UserId><Password>$pass</Password></AccessRequest>
<?xml version=\"1.0\"?><RatingServiceSelectionRequest xml:lang=\"en-US\"><Request>
<TransactionReference><CustomerContext>Rating and Service</CustomerContext><XpciVersion>1.0001</XpciVersion>
</TransactionReference><RequestAction>Rate</RequestAction><RequestOption>rate</RequestOption></Request>
<PickupType><Code>01</Code></PickupType><Shipment><Shipper><Address><PostalCode>97214</PostalCode>
</Address></Shipper><ShipTo><Address><PostalCode>$sZip</PostalCode><CountryCode>US</CountryCode>
</Address></ShipTo><Service><Code>$svccode</Code></Service><Package><PackagingType><Code>02</Code>
<Description>Package</Description></PackagingType><Description>Rate Shopping</Description><PackageWeight>
<Weight>$shipweight</Weight></PackageWeight></Package></Shipment></RatingServiceSelectionRequest>";

// 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.
     break;
    }

}

// clean up
xml_parser_free($xml_parser);
// should give us the price. 
if($stats>0){
  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."; } ? >