USPS Shipment Tracking with ColdFusion

 

The USPS tracking API is extremely simple. All you need is an API login from USPS and a cfhttp tag to get it to work. Well, that’s almost all you need...

I have put together a custom tag to track USPS shipping details. Copy the code at the end of this page, and then save it as... let’s go with USPSTracking.cfm. Next, call it from your main page using the following:

   1: <cf_USPSTracking
   2:  UserID = "[My USPS Login ID]"
   3:  Password = "[My USPS Password]"
   4:  TrackingID = "[My Tracking ID]" >
   5: <cfoutput>#tracking#</cfoutput>
Show/Hide Line Numbers . Full Screen . Plain

It will return the variable “tracking” containing the tracking details. There is an optional attribute [ShowDetail] that should be set to default as "Yes." This will enable full details about the tracking history. If it is set to "no," the tag will return only the final destination of the shipment.

Here is the code:

   1: <cfsilent>
   2: <cfparam name="attributes.UserID"    default="">
   3: <cfparam name="attributes.Password" default="">
   4: <cfparam name="attributes.TrackingID" default="">
   5: <cfparam name="attributes.ShowDetail" default="Yes">
   6: <cfset ApiUrl = "http://Production.ShippingAPIs.com/ShippingAPITest.dll?API=TrackV2">
   7: <!--- ************************************************ --->
   8: <!--- Stitch login details in to an xml string         --->
   9: <!--- ************************************************ --->
  10: <cfoutput>
  11: <cfsavecontent variable="xmlstring"><TrackRequest USERID="#attributes.UserID#" PASSWORD="#attributes.Password#"><TrackID ID="#rereplace(attributes.TrackingID,'[ \t\r\n]',"",'all')#"/></TrackRequest></cfsavecontent>
  12: </cfoutput>
  13: <!--- ************************************************ --->
  14: <!--- Call USPS over HTTP                             --->
  15: <!--- ************************************************ --->
  16: <cfhttp url="http://production.ShippingAPIs.com/ShippingAPI.dll?API=TrackV2&XML=#ToString(xmlstring)#" method="GET" timeout="10" />
  17: <cfif listfirst(cfhttp.statuscode,' ') eq 200>
  18:  <!--- Response Received             --->
  19:  <cfset response = XMLParse(cfhttp.fileContent)>
  20:  <cfif StructKeyExists(response,'error')>
  21:  <!--- USPS Return an Error     --->
  22:  <cfset tracking = "Tracking Number Error. Please make sure the tracking number is correct and try again.">
  23:  <cfelse>
  24:  <!--- Tracking Details Received --->
  25:  <cfset tracking = "<strong>#response.TrackResponse.TrackInfo.TrackSummary.XmlText#</strong>">
  26:  <cfif YesNoFormat(attributes.ShowDetail) and StructKeyExists(response.TrackResponse.TrackInfo,'TrackDetail')>
  27:     <cfloop from="1" to="#ArrayLen(response.TrackResponse.TrackInfo.TrackDetail)#" index="i">
  28:     <cfset tracking = "#tracking#<br />#response.TrackResponse.TrackInfo.TrackDetail[i].XmlText#">
  29:     </cfloop>
  30:  </cfif>
  31:  </cfif>
  32: <cfelse>
  33:  <!--- http request fail. Network Error --->
  34:  <cfset tracking = "Tracking services not available right now. Please Try Again Later">
  35: </cfif>
  36: <cfset caller.tracking = tracking>
  37: </cfsilent>
Show/Hide Line Numbers . Full Screen . Plain

(update June 02 2012) As a function:

   1: <cffunction name="USPSTracking" access="public" output="Yes">
   2: <cfargument name="TrackingID"required="no" default="">
   3: <cfargument name="ShowDetail"required="no" default="">
   4: <cfargument name="UserID"required="no" default="371DEVEL311">
   5: <cfargument name="Password"required="no" default="603QD95BW834">
   6: 
   7: <cfset local.ApiUrl = "http://Production.ShippingAPIs.com/ShippingAPITest.dll?API=TrackV2">
   8: <cfoutput><cfsavecontent variable="local.xmlstring"><TrackRequest USERID="#arguments.UserID#" PASSWORD="#arguments.Password#"><TrackID ID="#rereplace(arguments.TrackingID,'[ \t\r\n]',"",'all')#"/></TrackRequest></cfsavecontent></cfoutput>
   9: <cfhttp url="http://production.ShippingAPIs.com/ShippingAPI.dll?API=TrackV2&XML=#ToString(local.xmlstring)#" method="GET" timeout="10" />
  10: <cfset local.tracking= ArrayNew(1)>
  11: 
  12: <cfif listfirst(cfhttp.statuscode,' ') eq 200>
  13: <!--- Response Received             --->
  14: <cfset local.response = XMLParse(cfhttp.fileContent)>
  15: <cfif StructKeyExists(local.response,'error')>
  16: <!--- USPS Return an Error     --->
  17: <cfset ArrayAppend(local.tracking,"Tracking Number Error. Please make sure the tracking number is correct and try again.")>
  18: <cfelse>
  19: <!--- Tracking Details Received --->
  20: <cfset ArrayAppend(local.tracking,"#response.TrackResponse.TrackInfo.TrackSummary.XmlText#")>
  21: <cfif YesNoFormat(arguments.ShowDetail) and StructKeyExists(local.response.TrackResponse.TrackInfo,'TrackDetail')>
  22: <cfloop from="1" to="#ArrayLen(local.response.TrackResponse.TrackInfo.TrackDetail)#" index="i">
  23: <cfset ArrayAppend(local.tracking,"#trim(response.TrackResponse.TrackInfo.TrackDetail[i].XmlText)#")>
  24: </cfloop>
  25: </cfif>
  26: </cfif>
  27: </cfif>
  28: <cfreturn local.tracking>
  29: </cffunction>
Show/Hide Line Numbers . Full Screen . Plain

 

 

Name  
(required)
Email  
(required - never shown publicly)
Web Site  
Notify me of new comments via email.
Notify me of replies via email.