Welcome Guest, Not a member yet? Register   Sign In
Function to Retrieve FedEx Ground Shipping Rates
#1

[eluser]danoph[/eluser]
Here is code to retrieve fedex ground shipping rates instantly from using the API on their website.

The function as shown has the origin state and zip hardcoded in the function and will take destination state and zip as function arguments.

You need two of their API files for this to work, fedex.php and xmlparser.php. You can download those from http://bizwidgets.biz/downloads/fedex_api_files.zip. This example shows the two files being hosted in the libraries folder in a normal CodeIgniter project.

Important! If you need to expand upon this function and get rates for Express Shipping (like next day, two day, etc instead of ground shipping), you need to change the carrier code to FDXE instead of FDXG.

Code:
function ship_rate($dest_state = "", $dest_zip = "") {
        if (empty($dest_state) || empty($dest_zip)) {echo "0"; return; }
        $dest_state = strtoupper($dest_state);
        
        require_once(getcwd().'/system/application/libraries/fedex.php');

        $fedex = new Fedex;
        $fedex->setServer("https://gatewaybeta.fedex.com/GatewayDC");
        $fedex->setAccountNumber(123123123); // You need your own
        $fedex->setMeterNumber(123123123);    // You need your own
        $fedex->setCarrierCode("FDXG");
        $fedex->setDropoffType("REGULARPICKUP");
        $fedex->setService('FEDEXGROUND', 'FedEx Ground');
        $fedex->setPackaging("YOURPACKAGING");
        $fedex->setWeightUnits("LBS");
        $fedex->setWeight(3);
        $fedex->setOriginStateOrProvinceCode("MO");
        $fedex->setOriginPostalCode(63110);
        $fedex->setOriginCountryCode("US");
        $fedex->setDestStateOrProvinceCode($dest_state);
        $fedex->setDestPostalCode($dest_zip);
        $fedex->setDestCountryCode("US");
        $fedex->setPayorType("SENDER");

        $price = $fedex->getPrice();

        // echo "<pre>";
        //         print_r($price);
        // echo "</pre>";
        
        // echo "Price:".$price->price->rate;
        
        // print_r($price);
        
        //    markup shipping to cover handling cost
        // the following example marks up shipping 180% of the shipping rate.
        
        // $rate = ($price->price->rate + ($price->price->rate * 0.8));

        $rate = $price->price->rate;
        
        echo $rate; // you can either echo (i use this for ajax requests) or return the rate
        
        return $rate;

    }
#2

[eluser]helphelp[/eluser]
Thanks, although i am looking for UPS shipping. But your code still help a lot. I am wondering what does
"$rate = $price->price->rate;" do? I think you can just echo $price for the rate?

Thanks




Theme © iAndrew 2016 - Forum software by © MyBB