I only need to get the Fedex rates working. The script seems to be defaulting to the Fedex Ground rate. Can anyone figure out where or how this is happening? I would like to be able to change the default so the user could select Overnight, 2-Day,etc. Thank you in advance for your help.
function get_rates($weight, $destination_zipcode, $origin_zipcode, $carriers) {
$service = array();
if ($carriers['fedex'] && $fedex = get_fedex_rates($weight, $destination_zipcode, $origin_zipcode)) {
$service = array_merge_recursive($service, $fedex);
}
if ($carriers['ups'] && $ups = get_ups_rates($weight, $destination_zipcode, $origin_zipcode)) {
$service = array_merge_recursive($service, $ups);
}
if ($carriers['usps'] && $usps = get_usps_rates($weight, $destination_zipcode, $origin_zipcode)) {
$service = array_merge_recursive($service, $usps);
}
return $service;
}
function get_fedex_rates($weight, $destination_zipcode, $origin_zipcode) {
$weight = round($weight);
if ($weight < 1) {
$weight = 1.0;
} else {
$weight = number_format($weight,1);
}
$service = fedex_rateshop($weight, $destination_zipcode, $origin_zipcode);
return $service;
}
function usps_rateshop($weight, $destination_zipcode, $origin_zipcode) {
$request = "<RateRequest USERID=\"".USPS_LOGIN."\" PASSWORD=\"".USPS_PASSWORD."\">
<Package ID=\"0\">
<Service>Express</Service>
<ZipOrigination>$origin_zipcode</ZipOrigination>
<ZipDestination>$destination_zipcode</ZipDestination>
<Pounds>$weight</Pounds>
<Ounces>0</Ounces>
<Container>None</Container>
<Size>Regular</Size>
<Machinable>False</Machinable>
</Package>
<Package ID=\"1\">
<Service>Priority</Service>
<ZipOrigination>$origin_zipcode</ZipOrigination>
<ZipDestination>$destination_zipcode</ZipDestination>
<Pounds>$weight</Pounds>
<Ounces>0</Ounces>
<Container>None</Container>
<Size>Regular</Size>
<Machinable>False</Machinable>
</Package>
<Package ID=\"2\">
<Service>Parcel</Service>
<ZipOrigination>$origin_zipcode</ZipOrigination>
<ZipDestination>$destination_zipcode</ZipDestination>
<Pounds>$weight</Pounds>
<Ounces>0</Ounces>
<Container>None</Container>
<Size>Regular</Size>
<Machinable>False</Machinable>
</Package>
<Package ID=\"3\">
<Service>Library</Service>
<ZipOrigination>$origin_zipcode</ZipOrigination>
<ZipDestination>$destination_zipcode</ZipDestination>
<Pounds>$weight</Pounds>
<Ounces>0</Ounces>
<Container>None</Container>
<Size>Regular</Size>
<Machinable>False</Machinable>
</Package>
<Package ID=\"4\">
<Service>BPM</Service>
<ZipOrigination>$origin_zipcode</ZipOrigination>
<ZipDestination>$destination_zipcode</ZipDestination>
<Pounds>$weight</Pounds>
<Ounces>0</Ounces>
<Container>None</Container>
<Size>Regular</Size>
<Machinable>False</Machinable>
</Package>
<Package ID=\"5\">
<Service>Media</Service>
<ZipOrigination>$origin_zipcode</ZipOrigination>
<ZipDestination>$destination_zipcode</ZipDestination>
<Pounds>$weight</Pounds>
<Ounces>0</Ounces>
<Container>None</Container>
<Size>Regular</Size>
<Machinable>False</Machinable>
</Package>
</RateRequest>";
$xml = "API=Rate&XML=" . $request;
$string = USPS_SERVER.$xml;
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL,USPS_SERVER);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec ($ch);
curl_close ($ch);
return $result;
}
function fedex_rateshop($weight, $destination_zipcode, $origin_zipcode) {
if ($weight < 1) { $weight = 1.0; } else { $weight = number_format($weight, 1); }
$data = '0,"25"'; // TransactionCode
$data .= '10,"' . FEDEX_ACCOUNT . '"'; // Sender fedex account number
$data .= '498,"' . FEDEX_ACCESS_ID . '"'; // Meter number
$data .= '9,"' . $origin_zipcode . '"'; // Origin postal code
$data .= '117,"US"'; // Origin country
$data .= '17,"' . $destination_zipcode . '"'; // Recipient zip code
$data .= '50,"US"'; // Recipient country
$data .= '75,"LBS"'; // Weight units
$data .= '1116,"I"'; // Dimension units
$data .= '1401,"' . $weight . '"'; // Total weight
$data .= '1529,"1"'; // Quote discounted rates
$data .= '1273,"01"'; // Package type
$data .= '1333,"1"'; // Drop of drop off or pickup
$data .= '99,""'; // End of record
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, FEDEX_SERVER);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Referer: Sonicode
Host: " . FEDEX_SERVER,
"Accept: image/gif,image/jpeg,image/pjpeg,text/plain,text/html,*/*",
"Pragma:",
"Content-Type:image/gif"));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec ($ch);
curl_close ($ch);
return parse_fedex($result);
}
function parse_fedex($data) {
$service = false;
$fedex_service['01'] = 'FedEx Priority Overnight';
$fedex_service['05'] = 'FedEx Standard Overnight';
$fedex_service['03'] = 'FedEx 2-Day Air';
$fedex_service['20'] = 'FedEx Express Saver';
$fedex_service['92'] = 'FedEx Ground';
$current = 0;
$length = strlen($data);
$resultArray = array();
while ($current < $length) {
$endpos = strpos($data, ',', $current);
if ($endpos === FALSE) { break; }
$index = substr($data, $current, $endpos - $current);
$current = $endpos + 2;
$endpos = strpos($data, '"', $current);
$resultArray[$index] = substr($data, $current, $endpos - $current);
$current = $endpos + 1;
}
if (isset($resultArray[1133])) {
$i = 0;
$count = 1;
while ($count <= $resultArray[1133]) {
if (isset($fedex_service[$resultArray["1274-$count"]])) {
$service["carrier"][$i] = 'fedex';
$service["code"][$i] = $resultArray["1274-$count"];
$service["name"][$i] = $fedex_service[$resultArray["1274-$count"]];
$service["price"][$i] = $resultArray["1419-$count"];
$i++;
}
$count++;
}
}
return $service;
}
function traverseXMLIZE($array, $arrName = "array", $level = 0) {
if ( $level == 0 ) {
echo "<pre>";
}
while ( list($key, $val) = each($array) ) {
if ( is_array($val) ) {
traverseXMLIZE($val, $arrName . "[" . $key . "]", $level + 1);
} else {
echo '$' . $arrName . '[' . $key . '] = "' . $val . "\"\n";
}
}
if ( $level == 0 ) {
echo "</pre>";
}
}
function xml_depth($vals, &$i) {
$children = array();
if (isset($vals[$i]['value'])) array_push($children, $vals[$i]['value']);
while (++$i < count($vals)) {
switch ($vals[$i]['type']) {
case 'cdata':
array_push($children, $vals[$i]['value']);
break;
case 'complete':
$tagname = $vals[$i]['tag'];
if (isset($children["$tagname"])) {
$size = sizeof($children["$tagname"]);
} else {
$size = 0;
}
if (isset($vals[$i]['value'])) {
$children[$tagname][$size]["#"] = $vals[$i]['value'];
}
if(isset($vals[$i]["attributes"])) {
$children[$tagname][$size]["@"]
= $vals[$i]["attributes"];
}
break;
case 'open':
$tagname = $vals[$i]['tag'];
if (isset($children["$tagname"])) {
$size = sizeof($children["$tagname"]);
} else {
$size = 0;
}
if(isset($vals[$i]["attributes"])) {
$children["$tagname"][$size]["@"]
= $vals[$i]["attributes"];
$children["$tagname"][$size]["#"] = xml_depth($vals, $i);
} else {
$children["$tagname"][$size]["#"] = xml_depth($vals, $i);
}
break;
case 'close':
return $children;
break;
}
}
return $children;
}
function xmlize($data) {
$vals = $index = $array = array();
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, $data, $vals, $index);
xml_parser_free($parser);
$i = 0;
if (isset($vals[$i]['tag'])) {
$tagname = $vals[$i]['tag'];
if (isset($vals[$i]["attributes"])) {
$array[$tagname]["@"] = $vals[$i]["attributes"];
}
$array[$tagname]["#"] = xml_depth($vals, $i);
}
return $array;
}
?>