Today I will discuss how to use php class to calculate the UPS shipping Rate
Update: Although this code still works great, I have started an open source project at http://code.google.com/p/ups-php/. For easier to use code, improved documentation and a more complete feature set. The project also covers the other UPS API services.
Update 2: For a no fuss, 100% complete UPS Online Tools script with label printing support, integration support and automatic certification generation (required by UPS), I have created, RocketShipIt.
Comments have also been disabled, if you are having problems with the script please start an issue ticket at: http://code.google.com/p/ups-php
The UPS manual for XML shipping calculation is hundreds of pages long and unfortunately provides no examples written in PHP. Only ASP… Damn them! After hours of reading the UPS manual and lots of Google searching I figured out how to make my own and I thought I would share my method. So in today’s post we will look at an example ups rate calculation using PHP.
If you are looking for the United States Postal Service (USPS) function see, Calculating USPS Shipping Rates with PHP.
You can also integrate UPS Tracking features on your site once you get a handle on this.
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:
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.net:3128′);”
Help Program
I have started a UPS API open source project at, http://code.google.com/p/ups-php. If you would like to contribute and add to my code or help create more modules for the different UPS tools come on over.
source:http://www.marksanborn.net/php/calculating-ups-shipping-rate-with-php/
Related posts:












I love your website! did you create this yourself or did you outsource it? Im looking for a blog design thats similar so thats the only reason I’m asking. Either way keep up the nice work I was impressed with your content really..
It is nice to see this blog is finally getting the attention it totally deserves. Keep up the fantastic work.
Hey we was just viewing your page on my Nokia Phone and I was thinking about how well it will work on the new ipad thats coming out. Fleeting thought…. Anyway thanks!
I just wanted to comment and say that I really enjoyed reading your blog post here. It was very informative and I also digg the way you write! Keep it up and I’ll be back to read more in the future
Great information! I’ve been looking for something like this for a while now. Thanks!
Nice comments about proxies. Thanks! Would a proxy also be be worthwill for surfing the web?
thanks for the inspiration I was stressed by work but i learnt that life is about living to the fullest and enjoying every moment.Thanks a million
Easily, the post is actually the greatest on this deserving topic. I agree with your conclusions and will thirstily look forward to your coming updates. . . . .
Hello Guru, what entice you to post an article. This article was extremely interesting, especially since I was searching for thoughts on this subject last Thursday.
Good points…I would note that as someone who really doesn’t write on blogs much (in fact, this may be my first post), I don’t think the term “lurker” is very becoming to a non-posting reader. It’s not your fault really , but perhaps the blogosphere could come up with a better, non-creepy name for the 90% of us that enjoy reading the posts.
Thanks for some good points there. I am kind of new to online , so I printed this off to put in my file, any better way to go about keeping track of it then printing?
Nice brief and this fill someone in on helped me alot in my college assignement. Gratefulness you on your information.
Amiable fill someone in on and this fill someone in on helped me alot in my college assignement. Thanks you seeking your information.
Sorry for my bad english. Thank you so much for your good post. Your post helped me in my college assignment, If you can provide me more details please email me.