注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

天高云淡的博客

A bad beginning makes a bad ending.

 
 
 

日志

 
 

How to Calculate USPS Shipping Rates With PHP  

2010-06-07 19:54:04|  分类: USPS |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
  1. Step 1
    Calculate postage.
    stamp image by CraterValley Photo from Fotolia.com 
    Calculate postage.

    Sign up for an account to use the USPS Rate Calculator API (See Resources). Once you are registered, the USPS will provide you with a username and password. The confirmation email also contains the URL for the testing API server. Once you have tested your script and are ready to go live with it, you must contact the USPS Internet Customer Care Center to be granted access to the production server.

  2. Step 2

    Calculate the size and weight of your shipment based on customer input and your own records. For example, if you sell six types of widgets, you will need to figure the shipping weight and dimensions for different quantities of each of the items in your inventory. You may want to store this information in a database and retrieve the information based on the parameters of your customer's query.

  3. Step 3

    Use the shipment information from your database to generate an XML tree describing the shipment. Your XML tree must contain required elements and attributes, and may contain additional optional elements and attributes. The root node of the XML tree must be named RateV3Request, and must include the required attribute USERID. Within the root node, your XML string may include multiple Package nodes. Each Package node must have an ID attribute, although the format of the ID attribute is up to you.

  4. Step 4

    Include the required and optional child nodes of each Package node in your XML tree. The required nodes are Service, ZipOrigination, ZipDestination, Pounds, Ounces and Size. There are 13 different acceptable values for Service, as specified in the documentation (Ssee References). First class mail may weigh up to 13 ounces; other packages may weigh up to 70 pounds each. If you select First Class as the Service, you must specify the FirstClassMailType (either Letter, Flat or Parcel). ZIP codes must be valid five-digit zip codes. Values for pounds and ounces must be numeric; do not include a unit abbreviation in addition to the numeric value. There are several acceptable values for size: if you are sending a letter, you may leave this node value empty; if the length of the package plus its girth equals 84 inches or less, the value of the size node is Regular; otherwise it is Large or oversize.

  5. Step 5
    Type your computer code.
    clavier,internet image by yam from Fotolia.com 
    Type your computer code.

    Create an XML string from the XML tree you constructed in the previous steps. There are several methods for string conversion of an XML datatype; but perhaps the simplest method is to construct your tree in string format as demonstrated in the following example. First set the appropriate variables with your script, based on user selections and the response from your database, as described. Then use the variables to construct a URL. Note that the URL for the API Request in the example is specific to the production server, which you will need additional permission to access.

    <?php
    $packageID = 1;
    $service = 'ALL';
    $origin = '12345';
    $destination = '12345';
    $pounds = 0;
    $ounces = 13;
    $size = 'REGULAR';

    $XMLString = '<RateV3Request USERID="XXXXXXXXX"><Package ID="'.$packageID.'"><Service>'.$service.'</Service><ZipOrigination>'.$origin.'</ZipOrigination><ZipDestination>'.$destination.'</ZipDestination><Pounds>'.$pounds.'</Pounds><Ounces>'.$ounces.'</Ounces><Size>'.$size.'</Size></Package></RateV3Request>';

    $APIRequest = urlencode("http://production.shippingapis.com/ShippingAPI.dll?API=RateV3&XML=$XMLString");
    ?>

  6. Step 6

    Send the query and capture the response. The method may differ depending on your server environment and permissions; but one method is to treat the URL as a path to an XML document, and to read this XML document as a SimpleXMLElement object.

    $responseXML = new SimpleXMLElement($APIRequest, NULL, TRUE);

    This provides an easily parsed XML object including the postage rate for the service. For example:

    $postageNodes = $responseXML->xpath('Package/Postage');
    foreach($postageNodes as $node){
    echo 'The rate for '.$node->MailService.' is '.$node->Rate.'<br />';
    }

  评论这张
 
阅读(785)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017