2

I am having trouble using the FedEx API, I have read through the documentation pertaining to tracking packages. I have gotten all of the numbers I need to fill into the URL (Test tracking number, Account Number, Account Key, Meter Number, User Name and password).

I was given the URL to use from FedEx, "The production server URL is “https://ws.fedex.com:443/web-services”, however, I am also told to replace the credentials with my actual information (Test tracking number, Account Number, Account Key, Meter Number, User Name and password).

I found this code on another StackOverflow post, and I saw that there was a user who had commented that this way of accessing the FedEx xml was outdated. Anyway I included the code posted by Harshal_m_joshi and edited by houbysoft here (original post Tracking API for Fedex and UPS).

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.

I was wondering if anyone knew of the new way to do this, or could at least point me in the right direction. I am fairly new to API's only having worked with USPS and WUnderground, which were both easy as pi ;) Any help would be greatly appreciated.

Steve
  • 1,553
  • 2
  • 20
  • 29
Chris McCole
  • 59
  • 2
  • 15

1 Answers1

2

You are going to POST the data to https://ws.fedex.com:443/web-services

Your POST data should look like this...

<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns=\"http://fedex.com/ws/track/v5\">
   <soapenv:Header/>
   <soapenv:Body>
   <TrackRequest xmlns:ns=\"http://fedex.com/ws/track/v5\"  
 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"  
 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">
<WebAuthenticationDetail>
<UserCredential>
<Key>" .$CFG["fedex_key"]   ."</Key>
<Password>" .$CFG["fedex_password"]   ."</Password>
</UserCredential>
</WebAuthenticationDetail>
<ClientDetail>
<AccountNumber>" . $CFG["fedex_acct"] ."</AccountNumber>
<MeterNumber>" . $CFG["fedex_meter"] ."</MeterNumber>


" .$tracknum ."
trck 5 0 0 " .$tracknum ." TRACKING_NUMBER_OR_DOORTAG true

andyknas
  • 1,939
  • 2
  • 15
  • 29
  • Thank you for the feedback, I will be sure to try this out, I really am unsure of how to use SOAP, and I really haven't even used much in terms of php either. So far, I do not seem to enjoy it all that much, but it may be partially because I am just not a huge fan of web-development. Thank you so much for the help! – Chris McCole Oct 08 '17 at 05:09
  • The SDK documentation says you can also just use plain XML, without the SOAP envelope. But it doesn't give a separate url endpoint, and doesn't show any examples. I'm having trouble getting plain XML to work, will post a question for this separately. – Randy Gamage Feb 17 '21 at 15:42
  • @RandyGamage It's the same XML that I posted above. Just build the XML string and send with a HTTP POST. The whole SOAP deal is just FedEx being buzzword compliant. – andyknas Feb 17 '21 at 18:21