#native_company# #native_desc#
#native_cta#

Fedex Shipping Calculator

By Trevor Linton
on July 15, 2001

Version: 1.0

Type: Function

Category: Shopping Carts

License: BSD License

Description: Calculate Shipping Rates for any of Fedex’s Shipping Types including Express types and Ground types

function getFEDEXPrice ($shipType,$sendZipcode,$recieveZipcode,$recieveCountry,$weight) {	

	// FPO - Fedex Priority Overnight - $price1
	// FSO - Fedex Standard Overnight - $price2
	// F2D - Fedex 2-Day Standard - $price3
	// FES - Fedex Express Saver - $price4
	// FFO - Fedex First Overnight - $price5
	// FGD - Fedex Ground - $gndprice

	if($shipType != "FGD") {
		$tmp = "jsp_name=index&orig_country=US&language=english&portal=plain&account=&heavy_weight=NO&packet_zip=&hold_packaging=&orig_zip=$sendZipcode&dest_zip=$recieveZipcode&dest_country_val=U.S.A.&company_type=Express&packaging=1&weight=$weight&weight_units=lbs&dim_units=in&dim_length=&dim_width=&dim_height=&dropoff_type=4&submit_button=Get+Ratennnn";
		$request = "POST /servlet/RateFinderServlet HTTP/1.1nAccept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, */*nReferer: http://rate.dmz.fedex.com/servlet/RateFinderServletnAccept-Language: en-usnContent-Type: application/x-www-form-urlencodednAccept-Encoding: gzip, deflatenUser-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)nHost: rate.dmz.fedex.comnContent-Length: ".strlen($tmp)."nConnection: Keep-AlivenCache-Control: no-cachenn" .$tmp;

		$socket = fsockopen("rate.dmz.fedex.com", 80);

		fputs($socket, $request);

		while ($out = fread ($socket, 2048))
			$iBuffer .= $out;

		$preprice = addcslashes("</TD><TD BGCOLOR="", "^.$|()[]*+?{}") . ".*". addcslashes("" ALIGN=right class='resultstable'>", "^.$|()[]*+?{}");
		$postprice = addcslashes("</TD><TD>&nbsp;</TD><TD></TD><TD></TD></TR>", "^.$|()[]*+?{}");
		$price_match = '([0-9]+.[0-9]+)';

		$regexp = $preprice . $price_match . $postprice .".*".$preprice . $price_match . $postprice .".*". $preprice . $price_match . $postprice . ".*". $preprice . $price_match . $postprice  .".*".  $preprice . $price_match . $postprice;	
		ereg($regexp, $iBuffer, $regs);
		list(,$FPO_fedex, $FSO_fedex, $F2D_fedex, $FES_fedex,$FFO_fedex) = $regs;

		switch($shipType) {
			case 'FPO':
				return($FPO_fedex);
				break;
			case 'FSO':
				return($FSO_fedex);
				break;
			case 'F2D':
				return($F2D_fedex);
				break;
			case 'FES':
				return($FES_fedex);
				break;
			case 'FFO':
				return($FFO_fedex);
				break;
		}
	} else {
		$socket = fopen("http://grd.fedex.com/cgi-bin/rrr2010.exe?func=Rate&OriginZip=$sendZipcode&OriginCountryCode=US&DestZip=$recieveZipcode&DestCountryCode=US&Weight=$weight&WeightUnit=1&DimUnit=&Screen=Ground", "r");
				
		while ($out = fread ($socket, 2048))
			$iBuffer .= $out;
	
		$regexp  = addcslashes("<!TotalCharges>", "^.$|()[]*+?{}")."([0-9]+.[0-9]+)".addcslashes("<!/TotalCharges>", "^.$|()[]*+?{}");
		ereg($regexp,$iBuffer,$regs);
		list($gndprice) = $regs;
		
		return($gndprice);
	}
}