888-978-4030

Yahoo Store Real Time Shipping with USPS

usps-logo[1]

USPS real time rates are a feature that has been long desired, you can always pay a developer but here is a walk through. I was working on other projects and for a long time I had some issues with USPS’ API and it is because their testing server is not all that functional so after sending an email to USPS API support they put my user ID to the production server and I was getting rates! I have to give credit where credit is due and say that Mark Sanborn’s Blog helped me and gave me about 90% of the PHP code needed to get this feature working.

I have cleaned up Mark’s code to better fit the Yahoo store platform and to make it easier for YOU to add your options.

There are a few things to keep in mind with this code,

  1.  It is in copy and paste format and will work with the Yahoo web hosting PHP.
  2. You will need to go to USPS site to sign up for a user name and get off the testing server (will take an email or 2).
  3. Ship weight will be rounded up to the nearest whole number, so 2.3 lbs will be 3 lbs for the USPS rate. I have done some testing and found out that 2.3 lbs is the same price as 3 lbs.

And without farther ado here is the code!

  1. <?
  2. // ========== CHANGE THESE VALUES TO MATCH YOUR OWN ===========
  3.  
  4. $userName = "USERNAME"; // Your USPS Username
  5. $orgin_zip = "90210"; // location that you are shipping from.
  6. $add_rate = ""; // amount that you would like to add on top of usps rate, can be left blank
  7. // =============== DON’T CHANGE BELOW THIS LINE ===============
  8.  
  9. // ====== Yahoo Post Script Values ========
  10. $dest_zip = $_POST[‘Ship-Zip’];
  11. $weight = $_POST[‘Total-Weight’];
  12. $type = $_POST[‘Shipping’];
  13.  
  14. header("HTTP/1.0 200 OK"); //response the yahoo store needs to confirm
  15.  
  16. $url = "http://Production.ShippingAPIs.com/ShippingAPI.dll"; // production server and not test server.
  17.  
  18. $ch = curl_init();
  19. curl_setopt($ch, CURLOPT_URL,$url);
  20. curl_setopt($ch, CURLOPT_HEADER, 1);
  21. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  22. curl_setopt($ch, CURLOPT_POST, 1);
  23.  
  24. // This section of code is from http://www.marksanborn.net/php/calculating-usps-shipping-rates-with-php/ and is the master mind for the creation.
  25. // You can donate at http://www.marksanborn.net/donate… Please do if this helps your store.
  26.  
  27. $data = "API=RateV3&XML=<RateV3Request USERID=\"$userName\"><Package ID=\"1ST\"><Service>$type</Service><ZipOrigination>$orgin_zip</ZipOrigination><ZipDestination>$dest_zip</ZipDestination><Pounds>".ceil($weight)."</Pounds><Ounces>0</Ounces><Size>REGULAR</Size><Machinable>TRUE</Machinable></Package></RateV3Request>";
  28.  
  29. curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
  30. $result=curl_exec ($ch);
  31. $data = strstr($result, ‘<?’);
  32. $xml_parser = xml_parser_create();
  33. xml_parse_into_struct($xml_parser, $data, $vals, $index);
  34. xml_parser_free($xml_parser);
  35. $params = array();
  36. $level = array();
  37.  
  38. foreach ($vals as $xml_elem) {
  39.         if ($xml_elem[‘type’] == ‘open’) {
  40.                 if (array_key_exists(‘attributes’,$xml_elem)) {
  41.                         list($level[$xml_elem[‘level’]],$extra) = array_values($xml_elem[‘attributes’]);
  42.                 } else {
  43.                 $level[$xml_elem[‘level’]] = $xml_elem[‘tag’];
  44.                 }
  45.         }
  46.         if ($xml_elem[‘type’] == ‘complete’) {
  47.         $start_level = 1;
  48.         $php_stmt = ‘$params’;
  49.         while($start_level < $xml_elem[‘level’]) {
  50.                 $php_stmt .= ‘[$level['.$start_level.']]’;
  51.                 $start_level++;
  52.         }
  53.         $php_stmt .= ‘[$xml_elem[\'tag\']] = $xml_elem[\'value\'];’;
  54.         eval($php_stmt);
  55.         }
  56. }
  57. $price = $params[‘RATEV3RESPONSE’][’1ST’][’1′][‘RATE’];
  58. if($add_rate != ""){
  59.  $price = $price+$add_rate;
  60.  }
  61. header("Shipping-Charge: $price");
  62. ?>

With all of my code you will need to test and you can do that with the shipping and tax test in the store, you will also need to make sure that your shipping Methods in the store match that of USPS, here is the list and they ARE case-sensitive

  • FIRST CLASS
  • PRIORITY
  • EXPRESS
  • PARCEL

6 Comments

Natural

Posted October 7, 2011 at 7:02 am | Permalink

Where would one paste this code in a yahoo store?

adam75dfw

Posted January 26, 2012 at 6:24 pm | Permalink

I still don’t understand where I put this code in my store. Apparently it’s to be saved in my Yahoo host files as .php file? Is that correct? If so, where in my html or Shipping Manager do I reference the file?
If not, then how and where is the file to be saved and referenced?

I would love to make this work. Any help will be appreciated.

    E-comm

    Posted February 4, 2012 at 12:29 pm | Permalink

    so what you will need to do is go to the “web hosting control panel” from there click on “Manage” -> “file manager” then click on “PHP” in the upper right hand corner of the page under “create” copy and paste this code and change what you need to.

    If you are still having issues E-comm solution will be happy to install USPS basic script for you for $50, feel free to email info@e-commsolution.com

crazyschmiddy

Posted April 23, 2012 at 12:43 pm | Permalink

Hello
Quick question do you need MS Std or MS Pro account with API access.

Thanks,
Eric

You must log in to post a comment.