The script in its entirety--- It runs perfectly as a standalone file this way, but I ulitmately want to run it as a function to return total shipping charges.
<?
//create values for the variables
$from_zip="18436";
$to_zip="78708";
$weight="16";
$service_code="03";
// Ups Site Registration Codes
$userid = "###";
$userid_pass = "##";
$access_key = "####";
$activity = "activity";
/*
The following variables must be passed.
$from_zip - $to_zip - $weight
Pkg size is optional with the defaults set in
the if statements below. Easiest to modify these or weight
to adjust end price.
If package size is to be used the following variables must contain values $length - $width - $height
*/
if (!$length)
$length = 10;
if (!$width)
$width = 10;
if (!$height)
$height = 10;
if (!$weight)
$weight = 10;
$y = "<?xml version=\"1.0\"?><AccessRequest xml:lang=\"en-US\"><AccessLicenseNumber>$access_key</AccessLicenseNumber><UserId>$userid</UserId><Password>$userid_pass</Password></AccessRequest><?xml version=\"1.0\"?><RatingServiceSelectionRequest xml:lang=\"en-US\"><Request><TransactionReference><CustomerContext>Bare Bones Rate Request</CustomerContext><XpciVersion>1.0</XpciVersion></TransactionReference><RequestAction>Rate</RequestAction><RequestOption>Rate</RequestOption></Request><PickupType><Code>01</Code></PickupType><Shipment><Shipper><Address><PostalCode>$from_zip</PostalCode><CountryCode>US</CountryCode></Address></Shipper><ShipTo><Address><PostalCode>$to_zip</PostalCode><CountryCode>US</CountryCode></Address></ShipTo><ShipFrom><Address><PostalCode>$from_zip</PostalCode><CountryCode>US</CountryCode></Address></ShipFrom><Service><Code>$service_code</Code></Service><Package><PackagingType><Code>02</Code></PackagingType><Dimensions><UnitOfMeasurement><Code>IN</Code></UnitOfMeasurement><Length>$length</Length><Width>$width</Width><Height>$height</Height></Dimensions><PackageWeight><UnitOfMeasurement><Code>LBS</Code></UnitOfMeasurement><Weight>$weight</Weight></PackageWeight></Package></Shipment></RatingServiceSelectionRequest>";
// cURL ENGINE
$ch = curl_init(); /// initialize a cURL session
curl_setopt ($ch, CURLOPT_URL,"https://www.ups.com/ups.app/xml/Rate");
//CURLOPT_URL,"https://wwwcie.ups.com/ups.app/xml/Rate");
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "$y");
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$xyz = curl_exec ($ch);
curl_close ($ch);
$obj = new xml($xyz,"xml");
//a 7.5% fuel surcharge is added to the rate below
if (!$i)
{
$i = '0';
$shipping = $xml["RatingServiceSelectionResponse_RatedShipment_RatedPackage_TotalCharges"][0]->MonetaryValue[$i]*1.075. "\n";
echo "UPS Shipping To $to_zip = $".number_format($shipping,2)."<br>Rates include a 7.5% fuel surcharge currently added by UPS<br><br>";
$i++;
}
//Below is the xml parser code
class xml_container {
function store($k,$v) {
$this->{$k}[] = $v;
}
}
class xml {
var $current_tag=array();
var $xml_parser;
var $Version = 1.0;
var $tagtracker = array();
function startElement($parser, $name, $attrs) {
array_push($this->current_tag, $name);
$curtag = implode("_",$this->current_tag);
if(isset($this->tagtracker["$curtag"])) {
$this->tagtracker["$curtag"]++;
} else {
$this->tagtracker["$curtag"]=0;
}
if(count($attrs)>0) {
$j = $this->tagtracker["$curtag"];
if(!$j) $j = 0;
if(!is_object($GLOBALS[$this->identifier]["$curtag"][$j])) {
$GLOBALS[$this->identifier]["$curtag"][$j] = new xml_container;
}
$GLOBALS[$this->identifier]["$curtag"][$j]->store("attributes",$attrs);
}
}
function endElement($parser, $name) {
$curtag = implode("_",$this->current_tag); // piece together tag
if(!$this->tagdata["$curtag"]) {
$popped = array_pop($this->current_tag); // or else we screw up where we are
return; // if we have no data for the tag
} else {
$TD = $this->tagdata["$curtag"];
unset($this->tagdata["$curtag"]);
}
$popped = array_pop($this->current_tag);
if(sizeof($this->current_tag) == 0) return; // if we aren't in a tag
$curtag = implode("_",$this->current_tag); // piece together tag
// this time for the arrays
$j = $this->tagtracker["$curtag"];
if(!$j) $j = 0;
if(!is_object($GLOBALS[$this->identifier]["$curtag"][$j])) {
$GLOBALS[$this->identifier]["$curtag"][$j] = new xml_container;
}
$GLOBALS[$this->identifier]["$curtag"][$j]->store($name,$TD); #$this->tagdata["$curtag"]);
unset($TD);
return TRUE;
}
function characterData($parser, $cdata) {
$curtag = implode("_",$this->current_tag); // piece together tag
$this->tagdata["$curtag"] .= $cdata;
}
function xml($data,$identifier='xml') {
$this->identifier = $identifier;
// create parser object
$this->xml_parser = xml_parser_create();
// set up some options and handlers
xml_set_object($this->xml_parser,$this);
xml_parser_set_option($this->xml_parser,XML_OPTION_CASE_FOLDING,0);
xml_set_element_handler($this->xml_parser, "startElement", "endElement");
xml_set_character_data_handler($this->xml_parser, "characterData");
if (!xml_parse($this->xml_parser, $data, TRUE)) {
sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($this->xml_parser)),
xml_get_current_line_number($this->xml_parser));
}
// we are done with the parser, so let's free it
xml_parser_free($this->xml_parser);
} // end constructor: function xml()
}
?>