针对Fedex和UPS的跟踪API

时间:2011-05-04 07:25:02

标签: ups fedex

是否有适用于Fedex和UPS跟踪的javaScript API?请帮忙

3 个答案:

答案 0 :(得分:27)

我用谷歌搜索但却找不到任何东西。然后我在ROR

中由服务器端决定

这里是如何从他们的测试服务器获取UPS和Fedex xml请求和响应

对于Fedex:

track_no = '111111111111' (This is test track no)

此fedex的XML请求主体

xml_req = 

"<TrackRequest xmlns='http://fedex.com/ws/track/v3'><WebAuthenticationDetail><UserCredential><Key>YOUR_ACC_KEY</Key>
               <Password>YOUR_ACC_PASSWORD</Password></UserCredential></WebAuthenticationDetail><ClientDetail>
               <AccountNumber>YOUR_ACC_NUMBER</AccountNumber><MeterNumber>YOUR_ACC_METER_NUMBER</MeterNumber></ClientDetail>
               <TransactionDetail><CustomerTransactionId>ActiveShipping</CustomerTransactionId></TransactionDetail>
               <Version><ServiceId>trck</ServiceId><Major>3</Major><Intermediate>0</Intermediate><Minor>0</Minor></Version>
               <PackageIdentifier><Value>#{track_no}</Value><Type>TRACKING_NUMBER_OR_DOORTAG</Type></PackageIdentifier>
               <IncludeDetailedScans>1</IncludeDetailedScans></TrackRequest>"

path = "https://gatewaybeta.fedex.com:443/xml"

#this url connects to the test server of fedex
# for live server url is:"https://gateway.fedex.com:443/xml"

url = URI.parse(path)
http = Net::HTTP.new(url.host,url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE

response =  http.post(url.path, xml_req)
response_body = response.body
res = response_body.gsub(/<(\/)?.*?\:(.*?)>/, '<\1\2>')
hash = Hash.from_xml(res.to_s)

and that's it you will get response in hash variable, I converted xml response in to Hash because we can easily use Hash object at our view to display response data.

对于UPS:

track_no = '1Z12345E1512345676' (This is test track no)

此UPS

的XML请求正文
xml_req = 

'<?xml version="1.0"?><AccessRequest xml:lang="en-US"><AccessLicenseNumber>YOUR_ACC_LICENCE_NUMBER</AccessLicenseNumber>
                   <UserId>YOUR_ACC_USER_ID</UserId><Password>YOUR_ACC_PASSWORD</Password></AccessRequest>
                   <?xml version="1.0"?><TrackRequest xml:lang="en-US"><Request><TransactionReference>
                   <CustomerContext>QAST Track</CustomerContext><XpciVersion>1.0</XpciVersion></TransactionReference>
                   <RequestAction>Track</RequestAction><RequestOption>activity</RequestOption></Request>
                   <TrackingNumber>#{track_no}</TrackingNumber></TrackRequest>'

path = "https://www.ups.com/ups.app/xml/Track"
url = URI.parse(path)
http = Net::HTTP.new(url.host,url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE

response =  http.post(url.path, xml_req)
response_body = response.body
hash = Hash.from_xml(response_body.to_s)

此哈希变量包含以Hash格式显示的UPS跟踪请求的响应

答案 1 :(得分:21)

另一种简单的方法:只需使用以下href创建超链接

UPS:

http://wwwapps.ups.com/WebTracking/track?loc=en_US&track.x=Track&trackNums=put_tracking_number_here

FEDEX:

http://fedex.com/Tracking?action=track&language=english&cntry_code=us&tracknumbers=put_tracking_number_here

(不是那么优雅,但很快,很容易完成工作!)

答案 2 :(得分:1)

或者您可以使用active_shipping gem来更好,更清晰地跟踪Fedex和UPS的包裹