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/rate.htm

First steps:

  1. Register online (You will get your own user name)
  2. Test this script (You can use only test data)
  3. 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 = new USPS;
    $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:


    usps Object
    (
        [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 = new USPS;
    $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:


    usps Object
    (
        [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 = new USPS;
    $usps->setServer("http://testing.shippingapis.com/ShippingAPITest.dll");
    $usps->setUserName("??????????");
    $usps->setWeight(2, 0);
    $usps->setCountry("Albania");
    $price = $usps->getPrice();

Response 1:


    usps Object
    (
        [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:


    usps Object
    (
        [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.

61 Comments

  1. pawan says:

    thanks,Algirdas for reply, i want to know what is the process of implementation of usps after rate calculator.thanks

  2. pawan says:

    please provide me way of implementation(process) of usps after rate calculator.actully i want to know about reallty implementation of usps.thanks

  3. usmanabdulrazzaq says:

    Hi,

    Domestic rates work fine, but the international don’t.

    This is the error Message
    API Authorization failure. IntlRate is not a valid API name for this protocol.

    Thanks in Advnace

  4. Tim says:

    I’m also getting “Please enter a valid ZIP Code for the sender.” I’m on production, so why can’t I enter whatever zip codes I want?

    This is not working for me.

  5. Aiks says:

    It don’t work. Got the valid zip msg. Any idea?

  6. Kevin says:

    RE Implementation problems some are having: USPS returns an object, which can be converted into an array if you want to get up and running quickly. (PHP 5+ does not do this automatically anymore) Use the objectToArray function found here: http://www.phpro.org/examples/Convert-Object-To-Array-With-PHP.html. (Reposting due to cutoff text)

    USPS Rates

    setServer(“http://testing.shippingapis.com/ShippingAPITest.dll”);
    $usps->setUserName(“650SQUIS8177″);
    $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();
    print_r($price);
    $arrayprice=objectToArray($price); // convert object to array

    echo ‘This is the price for’ . $arrayprice['list'][1]['mailservice'] . ‘: ‘ . . ‘
    This is the price for ‘ . $arrayprice['list'][2]['mailservice'] . ‘ :’ . $arrayprice['list'][2]['rate'] . ”;
    ?>

  7. Alexey says:

    Hi,

    International rates don’t work :(

    This is the error Message
    API Authorization failure. IntlRate is not a valid API name for this protocol.

    Help please:)

  8. Manish says:

    I am also getting the same error..

    API Authorization failure. IntlRate is not a valid API name for this protocol.

    Please help…

  9. Mauricio says:

    International rates don’t work :(

    a blank page with USPS Rates title.

    please help..

  10. Mauricio says:

    hi, egain.

    international rates don’t work…

    USPS Object
    (
    [server] => http://testing.shippingapis.com/ShippingAPITest.dll
    [user] => xxxxxxxxxxx
    [pass] =>
    [service] =>
    [dest_zip] =>
    [orig_zip] =>
    [pounds] => 10
    [ounces] => 0
    [container] => None
    [size] => REGULAR
    [machinable] =>
    [country] => Albania
    [error] => error Object
    (
    [number] => 80040b1a
    [source] => UspsCom::DoAuth
    [description] => API Authorization failure. IntlRate is not a valid API name for this protocol.
    [helpcontext] =>
    [helpfile] =>
    )

    )

  11. am also getting the same error..

    API Authorization failure. IntlRate is not a valid API name for this protocol.

    Please help…

Leave a Reply