USPS 费率 API 仅适用于国内邮政编码

USPS Rate API Working for Domestic Zip Codes Only

我的 USPS 运费报价 api 仅适用于美国邮政编码。加拿大、英国和其他国际邮政编码导致此错误:Microsoft VBScript runtime error '800a01a8' Object required: xmlRate.selectSingleNode(...)' /folder/subfolder/usps_plugin.asp,第 32 行。

这是该文件的代码:

<%

function GetUSPSRate(SourceZip, DestZip, Weight, Method)
    dim xml
    dim post

    post = "<RateV4Request USERID='MYUSPSUSERNAME'>" & _
        "<Revision/>" & _
        "<Package ID='1ST'>" & _
        "<Service>" & Method & "</Service>" & _
        "<ZipOrigination>" & SourceZip & "</ZipOrigination>" & _
        "<ZipDestination>" & DestZip & "</ZipDestination>" & _
        "<Pounds>" & Weight & "</Pounds>" & _
        "<Ounces>0</Ounces>" & _
        "<Container>RECTANGULAR</Container>" & _
        "<Size>LARGE</Size>" & _
        "<Width>12</Width>" & _
        "<Length>12</Length>" & _
        "<Height>12</Height>" & _
        "<Girth>12</Girth>" & _
        "</Package>" & _
        "</RateV4Request>"

    set xml = CreateObject("MSXML2.ServerXMLHTTP")
    xml.open "GET", "http://production.shippingapis.com/ShippingAPI.dll?API=RateV4&XML=" & post
    xml.send("")

    dim xmlRate
    set xmlRate = xml.responseXML


    GetUSPSRate = xmlRate.selectSingleNode("//Rate").text
end function

function GetUSPSRates(SourceZip, DestZip, Weight)
    dim rates
    set rates = CreateObject("Scripting.Dictionary")
    rates.add "USPS Express", GetUSPSRate(SourceZip, DestZip, Weight, "EXPRESS")
    rates.add "USPS Priority", GetUSPSRate(SourceZip, DestZip, Weight, "PRIORITY")
'   rates.add "USPS First Class", GetUSPSRate(SourceZip, DestZip, Weight, "FIRST CLASS")

    set GetUSPSRates = rates
end function
%>

此外,这是调用该请求的代码:

dim uspsRates
dim uspsKey
set uspsRates = GetUSPSRates("02196", receiverpostalcode, shipmentweight)

for each uspsKey in uspsRates
    dim uspsValue
    uspsValue = uspsRates.item(uspsKey)
    %>
    <input name="shipinfo" type="radio" value="<%= uspsValue %>|<%= uspsKey %>">$<%= uspsValue + stone_handlingfee %>&nbsp;<%= uspsKey %>
    <br>
<% next
%>

对于国内邮政编码,它可以完美无缺地工作,但是,国际邮政编码是问题出现的时候。非常感谢建设性的意见。

生成国际报价时并未考虑所有变量。 Country、City 和 Zip 是最重要的。没有这些,api 就不会 return 结果。