Differences

This shows you the differences between two versions of the page.

Link to this comparison view

computers:php_fedex_shipping_calculator [2015/03/24 05:42] (current)
Line 1: Line 1:
 +====== PHP FedEx Shipping Calculator ======
 +Calculates FedEx shipping price using API by FedEx.
  
 +====== Code 1 ======
 +Reference: [[http://​www.phpbuilder.com/​snippet/​detail.php?​type=snippet&​id=413]]
 +
 +==== Usage Example ====
 +<code php> ​
 +// See function'​s name and variable on below Server Side code :)
 +
 +</​code>​
 +
 +==== Server Side Code  ====
 +
 +<code php>
 +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+Rate\n\n\n\n";​
 + $request = "POST /​servlet/​RateFinderServlet HTTP/​1.1\nAccept:​ image/gif, image/​x-xbitmap,​ image/jpeg, image/​pjpeg,​ application/​vnd.ms-excel,​ application/​msword,​ */​*\nReferer:​ http://​rate.dmz.fedex.com/​servlet/​RateFinderServlet\nAccept-Language:​ en-us\nContent-Type:​ application/​x-www-form-urlencoded\nAccept-Encoding:​ gzip, deflate\nUser-Agent:​ Mozilla/4.0 (compatible;​ MSIE 5.5; Windows 98)\nHost: rate.dmz.fedex.com\nContent-Length:​ "​.strlen($tmp)."​\nConnection:​ Keep-Alive\nCache-Control:​ no-cache\n\n"​ .$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);​
 + }
 +}
 +
 +</​code>​
 +
 +==== Client Side Code ====
 +Client side code is inherently insecure and error prone. ​ It should be used to reduce load on server, and input verified ​ by server. ​ Please include server verification code also in this section.
 +
 +Description,​ change title as needed
 +
 +<code javascript>​
 +code goes here
 +
 +</​code>​
 +
 +
 +====== Candidate Code Suggestions ======
 +Add your suggestions and links here.