Last active
May 16, 2018 16:29
-
-
Save cflove/6022405 to your computer and use it in GitHub Desktop.
USPS Tracking with ColdFusion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfcomponent displayname ="unit" output="no"> | |
<!--- ************************************************************************************ ---> | |
<!--- Get local.tracking ---> | |
<!--- ************************************************************************************ ---> | |
<cffunction name="tracking" access="remote" returntype="any" output="no"> | |
<cfargument name="trackingID" required="yes" type="string"> | |
<cfargument name="UserID" default=""> | |
<cfargument name="Password" default=""> | |
<cfargument name="ShowDetail" default="Yes"> | |
<cfset local.ApiUrl = "http://Production.ShippingAPIs.com/ShippingAPITest.dll?API=TrackV2"> | |
<!--- ************************************************ ---> | |
<!--- Stitch login details in to an xml string ---> | |
<!--- ************************************************ ---> | |
<cfoutput> | |
<cfsavecontent variable="local.xmlstring"> | |
<TrackRequest USERID="#arguments.UserID#" PASSWORD="#arguments.Password#"> | |
<TrackID ID="#rereplace(arguments.trackingID,'[ \t\r\n]',"",'all')#"/> | |
</TrackRequest> | |
</cfsavecontent> | |
</cfoutput> | |
<!--- ************************************************ ---> | |
<!--- Call USPS over HTTP ---> | |
<!--- ************************************************ ---> | |
<cfhttp url="http://production.ShippingAPIs.com/ShippingAPI.dll?API=TrackV2&XML=#ToString(local.xmlstring)#" method="GET" timeout="10" /> | |
<cfif listfirst(cfhttp.statuscode,' ') eq 200> | |
<!--- local.response Received ---> | |
<cfset local.response = XMLParse(cfhttp.fileContent)> | |
<cfif StructKeyExists(local.response,'error')> | |
<!--- USPS Return an Error ---> | |
<cfset local.tracking = "local.tracking Number Error. Please make sure the local.tracking number is correct and try again."> | |
<cfelse> | |
<!--- local.tracking Details Received ---> | |
<cfset local.tracking = "<strong>#local.response.trackresponse.TrackInfo.TrackSummary.XmlText#</strong>"> | |
<cfif YesNoFormat(arguments.ShowDetail) and StructKeyExists(local.response.trackresponse.TrackInfo,'TrackDetail')> | |
<cfloop from="1" to="#ArrayLen(local.response.trackresponse.TrackInfo.TrackDetail)#" index="i"> | |
<cfset local.tracking = "#local.tracking#<div>#local.response.trackresponse.TrackInfo.TrackDetail[i].XmlText#</div>"> | |
</cfloop> | |
</cfif> | |
</cfif> | |
<cfelse> | |
<!--- http request fail. Network Error ---> | |
<cfset local.tracking = "local.tracking services not available right now. Please Try Again Later"> | |
</cfif> | |
<cfreturn trim(local.tracking)> | |
</cffunction> | |
</cfcomponent> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment