PHP: USPS rates calculator
The USPS Web Tools allow developers of web-based and shrink-wrapped applications access to the on-line services of the United States Postal Service (USPS). They provide easy access to shipping information and services for your customers. Your customers can utilize the functions provided by the USPS without ever leaving your web site. Once the Web Tools are integrated, your server communicates through the USPS Web Tools server over HTTP using XML (eXtensible Markup Language).
Rates Calculators
Everything in details can be found here:
http://www.usps.com/webtools/htm/Rates-Calculatorsv1-0.htm
First steps:
- Register online (You will get your own user name)
- Test this script (You can use only test data)
- Contact ICCC and go live.
When you have completed your testing, email the USPS Internet Customer Care Center (ICCC). They will switch your profile to allow you access to the production server and will provide you with the production URL.
Domestic and International Rates
Domestic rates
Test 1:
$usps->setServer(“http://testing.shippingapis.com/ShippingAPITest.dll”);
$usps->setUserName(“?????????”);
$usps->setService(“PRIORITY”);
$usps->setDestZip(“20008″);
$usps->setOrigZip(“10022″);
$usps->setWeight(10, 5);
$usps->setContainer(“Flat Rate Box”);
$usps->setCountry(“USA”);
$price = $usps->getPrice();
Response 1:
(
[server] => http://testing.shippingapis.com/ShippingAPITest.dll
[user] => ???????????
[pass] =>
[service] => PRIORITY
[dest_zip] => 20008
[orig_zip] => 10022
[pounds] => 10
[ounces] => 5
[container] => Flat Rate Box
[size] => REGULAR
[machinable] =>
[country] => USA
[zone] => 3
[list] => Array
(
[0] => price Object
(
[mailservice] => “Priority Mail Flat Rate Box (11.25” x 8.75” x 6”)”
[rate] => 7.70
)
[1] => price Object
(
[mailservice] => “Priority Mail Flat Rate Box (14”x 12” x 3.5”)”
[rate] => 7.70
)
)
)
Test 2:
$usps->setServer(“http://testing.shippingapis.com/ShippingAPITest.dll”);
$usps->setUserName(“?????????”);
$usps->setService(“All”);
$usps->setDestZip(“20008″);
$usps->setOrigZip(“10022″);
$usps->setWeight(10, 5);
$usps->setContainer(“Flat Rate Box”);
$usps->setCountry(“USA”);
$usps->setMachinable(“true”);
$usps->setSize(“LARGE”);
$price = $usps->getPrice();
Response 2:
(
[server] => http://testing.shippingapis.com/ShippingAPITest.dll
[user] => ???????????
[pass] =>
[service] => All
[dest_zip] => 20008
[orig_zip] => 10022
[pounds] => 10
[ounces] => 5
[container] => Flat Rate Box
[size] => LARGE
[machinable] => true
[country] => USA
[zone] => 3
[list] => Array
(
[0] => price Object
(
[mailservice] => “Express Mail to PO Addressee”
[rate] => 39.20
)
[1] => price Object
(
[mailservice] => “Priority Mail”
[rate] => 8.95
)
[2] => price Object
(
[mailservice] => “Parcel Post”
[rate] => 7.80
)
[3] => price Object
(
[mailservice] => “Bound Printed Matter”
[rate] => 3.53
)
[4] => price Object
(
[mailservice] => “Media Mail”
[rate] => 5.14
)
[5] => price Object
(
[mailservice] => “Library Mail”
[rate] => 4.91
)
)
)
International rates:
Test 1:
$usps->setServer(“http://testing.shippingapis.com/ShippingAPITest.dll”);
$usps->setUserName(“??????????”);
$usps->setWeight(2, 0);
$usps->setCountry(“Albania”);
$price = $usps->getPrice();
Response 1:
(
[server] => http://testing.shippingapis.com/ShippingAPITest.dll
[user] => ?????????
[pass] =>
[service] =>
[dest_zip] =>
[orig_zip] =>
[pounds] => 2
[ounces] => 0
[container] => None
[size] => REGULAR
[machinable] =>
[country] => Albania
[list] => Array
(
[0] => intprice Object
(
[id] => 0
[rate] => 87
[pounds] => 2
[ounces] => 0
[mailtype] => “Package”
[country] => “ALBANIA”
[svccommitments] => “See Service Guide”
[svcdescription] => “Global Express Guaranteed (GXG) Document Service”
[maxdimensions] => “Max. length 46”, depth 35”, height 46” and max. girth 108””
[maxweight] => 22
)
[1] => intprice Object
(
[id] => 1
[rate] => 96
[pounds] => 2
[ounces] => 0
[mailtype] => “Package”
[country] => “ALBANIA”
[svccommitments] => “See Service Guide”
[svcdescription] => “Global Express Guaranteed (GXG) Non-Document Service”
[maxdimensions] => “Max. length 46”, depth 35”, height 46” and max. girth 108””
[maxweight] => 22
)
)
)
Possible errors:
(
[server] => http://testing.shippingapis.com/ShippingAPITest.dll
[user] => ???????????
[pass] =>
[service] => Alll
[dest_zip] => 20008
[orig_zip] => 10022
[pounds] => 10
[ounces] => 5
[container] => Flat Rate Box
[size] => LARGE
[machinable] => true
[country] => USA
[error] => error Object
(
[number] => -2147219487
[source] => Rate_Respond;SOLServerRatesTest.RateV2_Respond
[description] => “Invalid value for package size.”
[helpcontext] => 1000440
[helpfile] =>
)
)
Downlod this freeware PHP script: USPS rates calculator v1.2
Please make a donation of 1$ if you find our software useful and want to support the continued development.
Thank you.
19 Responses to 'PHP: USPS rates calculator'
Leave a Reply
You must be logged in to post a comment.
on October 11th, 2006 at 1:11 am
First of all thanks for the code - it helped me a lot to get started!
Now, I am wondering if I am doing something wrong here:
I’m on the production server now and when I try to send a request with an invalid zip code I don’t get an error but just no data, or when I enter a country with a space (like United States) I also get no data back.
Same effect if I try to limit the services from ‘All’ to something like PRIORITY - no data, and also no error message.
Any idea?
Thanks
Gunter
on October 11th, 2006 at 8:35 am
There was a bug when you enter country name with space but few days ago I have fixed it and now you should get data from USPS.
Also thank you for reporting another bug. Now when you put invalid zip code, there will be an error message.
on October 11th, 2006 at 8:38 am
Also when you are trying to get domestic rates, you shoud set “USA” as a country (not United states)
on September 11th, 2007 at 2:35 am
[…] USPS rate quotes using Aldirgas Varnagiris’ USPS PHP rates calculator. It worked perfectly right away, however you can only run the specific examples (as opposed to […]
on January 10th, 2008 at 2:19 am
Hi,
I just downloaded the USPS rate calculation software for my PHP 4.4.4 installation on Fedora Core 6 Linux. I have error reporting to E_ALL and when I visit index.php, I get these warnings…
Notice: Undefined index: HELPCONTEXT in /usr/local/apache2/htdocs/dhlovelife/v2/USPS/usps.php on line 105
Notice: Undefined index: HELPFILE in /usr/local/apache2/htdocs/dhlovelife/v2/USPS/usps.php on line 106
Is this a problem? Can it be fixed? Thanks, - Dave
on January 10th, 2008 at 10:13 am
Actually it’s not a problem, just a notice. And you should use error reporting only for testing reasons, when you will go LIVE, you should disable error reporting at all!
on January 21st, 2008 at 1:37 am
Algirdas,
I know that this post will make me sound like an idiot and that’s ok. I have been programming with PHP and MySQL for a few years now and I never botherd to learn object oriented programming because all of the people that had left posts all over the net saying that it slowed things down a bit. What I am trying to do now is figure out how to get all of the mailing services and their rates to show up in a drop box and I can’t even figure out how to get it to display only one out of the array in the index.php. Any help, advice or maybe some sample code would really be appreciated.
Now I am going to go and learn a little more.
Thanks,
gknight
on January 22nd, 2008 at 4:14 am
A bit of a suggestion. You might want to add into the text above that before you can change any of the information in the sample that you have given, you need to get the production access from USPS first. Took me all this time and having to contact one of the other posters here to figure that one out. Kinda stupid on USPS’s part, but I guess they do it for a reason.
Thanks again for posting this script.
gknight
on January 22nd, 2008 at 8:44 am
Maybe it’s not clear but there’s written: “Test this script (You can use only test data)”
on April 4th, 2008 at 3:38 pm
Hi,
Algirdas, I have downloaded this class. its working fine for me but when i change orign and destination zip codes from “20008″ and “10022″ to some valid zip codes like 07481 or 11375 or someone else.I just copy the files in my root folder and change password. Any help????
Thanks
on April 4th, 2008 at 3:40 pm
Hi,
Discard my previous posting…Sorry for that
Algirdas, I have downloaded this class. its working fine for me but when i change orign and destination zip codes from “20008″ and “10022″ to some valid zip codes like 07481 or 11375 or someone else, it shows following error:
[error] => error Object
(
[number] => -2147219490
[source] => Rate_Respond;SOLServerRatesTest.RateV2_Respond
[description] => Invalid value for origin ZIP Code.
[helpcontext] => 1000440
[helpfile] =>
)
I just copy the files in my root folder and change userid. Any help????
Thanks
on April 4th, 2008 at 3:44 pm
zip codes “20008″ and “10022″ is for testing. So if you have changed them and to other valid zip codes and your script stopped working, that could mean your USPS account is not changed to LIVE, with TEST account you can use only test zip codes and other test data.
on April 4th, 2008 at 4:14 pm
Thanks,
You mean to say that i must go to LIVE for getting desired result and as I m using it with test URL so that these problems occured??
OK i understand. Thanks for your help. Please also guide me what i have to do after i got my Production key activated??
Thanks again
on April 15th, 2008 at 7:39 pm
Hi,
Thanks for the script. I have a problem though, the domestic rates work just fine, but the international ones don’t, not on testings nor production.
This is the error I get: API Authorization failure. IntlRate is not a valid API name for this protocol.
I would really appreciate if you could help me, this is very important and urgent to me.
Hope you can give me a reply. THANKS A LOT!
Best regards
on June 23rd, 2008 at 4:00 pm
Has anyone used this on php5? How about the new V3 servers at USPS?
John
on September 24th, 2008 at 1:48 pm
Hi,
I have USPS realtime calculation API for both Domestic and International. It works fine with weight,zip code,country….But my problem is how can i use Dimension like length,height and width of product to calculate exact charges for both. I have my USPS username and password. Currently my API is working on production server. can u please give little bit code to use Dimension. Do u have any idea how to get Consignment No from USPS too..?
I need real help..? Pl help me in this issue….
waiting for your comment or reply…..
on October 31st, 2008 at 12:01 am
How would you display just the rate?
I have tried
$price = $usps->getPrice();
$total = $price->price->rate;
echo $total;
and it doesn’t work.
on October 31st, 2008 at 1:07 pm
Hello brooksh,
with this you get the response and the list of available methods and its prices:
so to print the method name and price you should write this:
{
echo $method->mailservice;
echo $method->price;
echo “\n“;
}
on November 6th, 2008 at 2:23 pm
Hello,
I saw someone ask a little while ago about updates for the new V3 system, it seems as i have been getting some strange shipping prices on my site, and i think the old version could be at fault. who knows with the usps though ;)
thanks in advance for the help!
-steve