Posted By

drifterz28 on 08/05/11


Tagged

yahoo store


Versions (?)

USPS Real time rates


 / Published in: PHP
 

URL: http://e-commsolution.com/usps-real-time-shipping/

  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. curl_close($ch);
  58. $price = $params['RATEV3RESPONSE']['1ST']['1']['RATE'];
  59. if($add_rate != ""){
  60. $price = $price+$add_rate;
  61. }
  62. header("Shipping-Charge: $price");
  63. ?>

Report this snippet  

You need to login to post a comment.