Header Ad

Wednesday, March 31, 2010

Calculating UPS Shipping Rate with PHP

This is one of the post which i came across for calculating the UPS Shipping charges in PHP.

Please find the code at http://code.google.com/p/ups-php

Things you will need

* UPS Online Tool Account – free but requires registration
* UPS Access Key – Comes with the online tool account
* cURL – Most LAMP (Linux, Apache, Mysql, PHP) web hosts have this installed by default.
* PHP – You must know a little bit about using php. After all you are about to create a custom UPS shipping calculator.
* XML – If you know what PHP is you should know what this is. If not read about it here.
* SSH access – Many web hosts offer this. You don’t HAVE to have it but it makes troubleshooting easier.

Basically you start off by getting setup with UPS and grabbing a copy of their XML manual. Now you need to construct an XML file that will be later sent off to UPS’s server. UPS will then send a response back to you in XML format. We then use PHP to turn the XML into variables that we can use and manipulate. Even add the shipping cost into the subtotal or final checkout cost. After all, whats the use of calculating UPS shipping if you are not going to charge the user for it.

Copy the UPS PHP Function I have created into a new .txt file and name it ups.php.

Go through this function and find the parts in the XML file that are in CAPS, You will need to add your UPS shipper number, access key, username, password, departure zip, etc…

Then create another .php file and make sure to include ups.php.

It might look something like this:
view plaincopy to clipboardprint?

1. require_once("ups.php")

require_once("ups.php")

Now all you have to do to get an accurate UPS rate for a package is call the ups function.

The basic syntax for the function is:
view plaincopy to clipboardprint?

1. ups($dest_zip,$service,$weight,$length,$width,$height)

ups($dest_zip,$service,$weight,$length,$width,$height)

So, for example if you wanted to send a five pound package to Beverly Hills CA using UPS ground you would do this:
view plaincopy to clipboardprint?

1. ups(90210,03,5,5,5,5)

ups(90210,03,5,5,5,5)

Here is an already written php test file that uses the ups function.

By now you may be wondering how I know that 03 is the code for UPS ground and you also may be wondering what the code is for 2nd Day Air.

You can find all this information in the huge manual with the name dtk_RateXML_V1.zip. You can find this manual on the UPS website after you login under UPS Rates and Services Selection but to save you the time I have listed them below.
UPS Service Selection Codes

* 01 – UPS Next Day Air
* 02 – UPS Second Day Air
* 03 – UPS Ground
* 07 – UPS Worldwide Express
* 08 – UPS Worldwide Expedited
* 11 – UPS Standard
* 12 – UPS Three-Day Select
* 13 Next Day Air Saver
* 14 – UPS Next Day Air Early AM
* 54 – UPS Worldwide Express Plus
* 59 – UPS Second Day Air AM
* 65 – UPS Saver

Feel free to edit the function. The XML part of the code should be altered to fit your needs. This function only has the basic elements needed to calculate a rate. There are other attributes you can specify in the XML part of the document, such as pickup type, packaging type, etc…

Unlike some UPS Rate calculators you do not need to update the fuel surcharges or anything else. These rates come straight from UPS and are as accurate as your XML file has specified.
Troubleshooting


If it’s not working and you think you may have messed up the PHP code you can test to see if it is working by sending your XML file with curl.

Log into your host via SSH, make an xml file named RequestXML.txt. Then run this command.

curl -d @RequestXML.txt https://www.ups.com/ups.app/xml/Rate

If you want to use UPS’s testing server you can use this URL:

https://wwwcie.ups.com/ups.app/xml/Rate
Special Instructions for Godaddy


Godaddy requires a minor change in the ups function. After reading this article:

You need to have a proxy to use cURL on Godaddy.

So under this line:
“curl_setopt($ch,CURLOPT_TIMEOUT, 60);”

add this:
“curl_setopt($ch,CURLOPT_PROXY,’http://proxy.shr.secureserver

No comments: