Fedex 和 UPS 的追踪 API

发布于 2024-11-05 03:23:28 字数 1539 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

她如夕阳 2024-11-12 03:23:28

我用谷歌搜索了类似的东西但找不到任何东西。然后我决定在 ROR 的服务器端进行。

这里介绍了如何从测试服务器获取 UPS 和 Fedex xml 请求和响应。

对于联邦快递:

track_no = '111111111111'  # This is a test tracking number

# This XML Request body for fedex
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)

就是这样!您将在哈希变量中获得响应,我将 xml 响应转换为 Hash,因为我们可以轻松地在视图中使用 Hash 对象来显示响应数据。

对于 UPS:

track_no = '1Z12345E1512345676'  # This is a test tracking number

# This XML Request body for UPS
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)

此哈希变量包含哈希格式的 UPS 跟踪请求的响应。

I googled for something like this but couldn't find anything. Then I decided to do it server side in ROR.

Here it is how to get UPS and Fedex xml request and response from their test servers.

For Fedex:

track_no = '111111111111'  # This is a test tracking number

# This XML Request body for fedex
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.

For UPS:

track_no = '1Z12345E1512345676'  # This is a test tracking number

# This XML Request body for UPS
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)

This hash variable contains the response of UPS Tracking Request in Hash format.

筱果果 2024-11-12 03:23:28

另一种简单的方法是:只需使用以下 href

UPS 创建一个超链接:

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

联邦快递:

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

(不那么优雅,但快速、简单,完成工作! )

another easy way to do it: Just create a hyperlink with the following 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

(not as elegant, but quick, easy and gets the job done!)

黎歌 2024-11-12 03:23:28

或者,您可以使用 active_shipping gem 以更好、更干净的方式跟踪 Fedex 和 UPS 的包裹

Or you can use the active_shipping gem for a nicer and cleaner way to track your packages for Fedex and UPS

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文