UPS Tracking with XML and ColdFusion CFC
04/06/2006 10:45:04 PM
ColdFusion CFCs or ColdFusion components are very usefule when it comes to coding. It goes beyond Custom Tags in their functionality. I put together this CFC to perform UPS tracking and return the data into a structure that can be referenced to display the tracking information for a package. I borrowed most of the code and adapted for my own use in an eCommerce application that I have developed.
First we declare the opening tags for the component:
<cffunction name="Tracking" access="public" returntype="struct">
<cfargument name="TrackingNumber" type="string" required="yes">
The first thing within the CFC I do is allocate a struct called TrackingInfo to store variable for the return to the calling procedure:
<cfset TrackingInfo = structnew()>
Next we build the XML request to UPS with the cfsavecontent tag:
<?xml version="1.0" ?>
<?xml version="1.0" ?>
The varable TrackingNumber is the UPS Tracking Number that you pass in. This chunk of code is saved in the variable XMLToSend. Next thing is to pass the code to UPS using the cfhttp tag to pass the information:
<cfhttp url="https://wwwcie.ups.com/ups.app/xml/Track" method="post">
<cfhttpparam name="Content-Type" type="HEADER" value="content-type: text/xml; charset='UTF-8';">
<cfhttpparam type="body" value="#XMLToSend#">
<cfset response = cfhttp.fileContent>
The response is saved in the variable response for further processing. We use the XMLParse function tag to create a parseable XML string. ColdFusion has many powerful XML parsing functions built in and you still need to know the structure of the returning XML to really get at the data you want.
<cfset StartingPosition = Find('<TrackResponse>',response)>
<cfset XMLResponse = XMLParse(Mid(response,StartingPosition,(Len(response)+1)-StartingPosition))>
<cfset trackXML = XMLResponse>
<cfset TrackingInfo.ResponseStatusCode = xmlResponse.TrackResponse.Response.ResponseStatusCode.XMLText>
Here we've retrieved the ResponseStatusCode and stored it into the TrackingInfo struct.
A ResponseStatusCode of 1 means that we had a successful communication and that tracking data is available. This next chuck of code saves the appropriate return variables in the TrackingInfo struct. If we did not receive a 1 in ResponseStatusCode, we just dummy up the remaining variables in the struct.
<cfif TrackingInfo.ResponseStatusCode eq "1">
<cfset TrackingInfo.rcvStatusCode = xmlResponse.TrackResponse.Shipment.Package.Activity.Status.StatusType.Code.XMLText>
<cfset TrackingInfo.rcvStatus = xmlResponse.TrackResponse.Shipment.Package.Activity.Status.StatusType.Description.XMLText>
<cfset rcvDate = xmlResponse.TrackResponse.Shipment.Package.Activity.Date.XMLText>
<cfset rcvTime = xmlResponse.TrackResponse.Shipment.Package.Activity.Time.XMLText>
<cfset TrackingInfo.rcvSigner = xmlResponse.TrackResponse.Shipment.Package.Activity.ActivityLocation.SignedForByName.XMLText>
<cfset TrackingInfo.rcvSigner = "">
<cfset TrackingInfo.LocationCity = xmlResponse.TrackResponse.Shipment.Package.Activity.ActivityLocation.Address.City.XMLText>
<cfset TrackingInfo.LocationState = xmlResponse.TrackResponse.Shipment.Package.Activity.ActivityLocation.Address.StateProvinceCode.XMLText>
<cfset TrackingInfo.LocationCountry = xmlResponse.TrackResponse.Shipment.Package.Activity.ActivityLocation.Address.CountryCode.XMLText>
<cfset TrackingInfo.shipService = xmlResponse.TrackResponse.Shipment.Service.Description.XMLText>
<cfset TrackingInfo.shipWeight = xmlResponse.TrackResponse.Shipment.Package.PackageWeight.Weight.XMLText>
<cfset TrackingInfo.shipMeasure = xmlResponse.TrackResponse.Shipment.Package.PackageWeight.UnitOfMeasurement.Code.XmlText>
<cfset tempPickup = xmlResponse.TrackResponse.Shipment.PickupDate.XMLText>
<cfset TrackingInfo.rcvDate = Mid(rcvDate,5,2) & "/" & Mid(rcvDate,7,2) & "/" & mid(rcvDate,1,4)>
<cfset TrackingInfo.rcvTime = Mid(rcvTime,1,2) & ":" & Mid(rcvTime,3,2) & ":" & mid(rcvTime,6,2)>
<cfset TrackingInfo.shipPickup = CreateDate(Left(tempPickup,4),Mid(tempPickup,5,2),Right(tempPickup,2))>
<cfset TrackingInfo.rcvStatusCode = "">
<cfset TrackingInfo.rcvStatus = "DATA UNAVAILABLE">
<cfset rcvDate = "">
<cfset rcvTime = "">
<cfset TrackingInfo.rcvSigner = "">
<cfset TrackingInfo.LocationCity = "">
<cfset TrackingInfo.LocationState = "">
<cfset TrackingInfo.LocationCountry = "">
<cfset TrackingInfo.shipService = "">
<cfset TrackingInfo.shipWeight = "">
<cfset TrackingInfo.shipMeasure = "">
<cfset TrackingInfo.rcvDate = "">
I have tried to implement this and the code is not working correctly.
I'm getting error:
Element TRACKRESPONSE.SHIPMENT.PACKAGE.ACTIVITY.ACTIVITYLOCATION.ADDRESS.CITY.XMLTEXT is undefined in XMLRESPONSE.
I tried to use the code EXACTLY as you have it below except using our own access and license key which we use in the UPS shipping calc XML that works well , but for some reason we cannot get this to work. When I put in the test tracking number etc and run it, all I get is a blamk screen, even with debugging switched on
As author of the above code I have revised the above code to include the actual CFC wrappers and invoke code. These are critial elements to getting this to work. Also, you must have the access code and login information from UPS.
Thanks for your comments.
This works great now with Tracking Numbers however when I replace it with the ReferenceNumber and change the CFC accordingly by inserting :
and supplying the reference number it does not work BUT I have an HTML form that does work with the reference number so I know that it is valid
it does not appear to work
This works great now with Tracking Numbers however when I replace it with the ReferenceNumber and change the CFC accordingly by inserting
There is also the CFGRIDUPDATE tag, however, if you have multiple field keys, this probably will not work.
This works great now with Tracking Numbers however when I replace it with the ReferenceNumber and change the CFC accordingly by inserting : #RefNumber# and supplying the reference number it does not work BUT I have an HTML form that does work with the reference number so I know that it is valid it does not appear to work