Tracking a Package


Most online stores offer the ability to track purchased items from within the store's website. This functionality can be achieved with a FDXTrackRequest request. The request will look something like this:

 $request = <<< XMLREQUEST <?xml version="1.0" encoding="UTF-8" ?>   <FDXTrackRequest xmlns:api="http://www.fedex.com/fsmapi"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:noNamespaceSchemaLocation="FDXShipRequest.xsd">   <RequestHeader>     <AccountNumber>$accountNumber</AccountNumber>     <MeterNumber>$meterNumber</MeterNumber>     <CarrierCode>$carrier</CarrierCode>   </RequestHeader>   <PackageIdentifier>     <Value>470034028693</Value>     <Type>TRACKING_NUMBER_OR_DOORTAG</Type>   </PackageIdentifier>   <DestinationCountryCode>US</DestinationCountryCode>   <DetailScans>1</DetailScans> </FDXTrackRequest> XMLREQUEST; 

Apart from the tracking number, the interesting piece of information here is DetailScans. A value of 1 indicates that all scans for the package should be returned (being picked up, arriving at the sorting facility, leaving the sorting facility, and so on) rather than just the most recent scan (which would be a value of 0).

Absent from the sample request are ShipDateRangeBegin and ShipDateRangeEnd or ShipDate. These elements give the API server more information on when the shipment was made to make its job of finding the tracking information easier. They are not required, but are recommended. If your application stores this information, it would be a good idea to include it in tracking requests (you only need the start and end of a range, or the exact [+– 5] ship date, not both). The dates follow the same format of other dates with this API, YYYY-MM-DD.

Also absent is the PagingToken value, which should always be empty for an initial request. If the higher detail level has been requested, and the shipment has a lot of scan points, it may exceed the maximum length of the response document, in which case not all scans will be reported in the initial request. In that case, a PagingToken value will be returned with the truncated request. That same value should be passed on in a subsequent FDXTrackRequest request to obtain further values.

The response should look something like this (this sample response is unrelated to the previous examples):

 <?xml version="1.0" encoding="UTF-8"?> <FDXTrackReply xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FDXTrackReply.xsd"> <ReplyHeader>   <CustomerTransactionIdentifier>FDXTrack</CustomerTransactionIdentifier> </ReplyHeader> <MoreData>false</MoreData> <TrackProfile>   <TrackingNumber>643133236401</TrackingNumber>   <CarrierCode>FDXE</CarrierCode>   <ShipDate>2006-01-21</ShipDate>   <DestinationAddress>     <City>DETROIT</City>     <StateOrProvinceCode>MI</StateOrProvinceCode>     <PostalCode>48243</PostalCode>     <CountryCode>US</CountryCode>   </DestinationAddress>   <DeliveredDate>2006-01-22</DeliveredDate>   <DeliveredTime>09:53</DeliveredTime>   <SignedForBy>R.Smith</SignedForBy>   <DeliveredLocationCode>1</DeliveredLocationCode>   <DeliveredLocationDescription>Front Desk</DeliveredLocationDescription>   <Service>Priority Pak</Service>   <Weight>4.0</Weight>   <WeightUnits>LBS</WeightUnits> <FedExURL>http://fedex.com/cgibin/tracking?action=track&amp;language=english&amp;cn     try_code=us&amp;initial=x&amp;tracknumbers=123456789123456</FedExURL>   <ScanCount>5</ScanCount>   <Scan>     <Date>2006-01-11</Date>     <Time>09:56</Time>     <ScanType>20</ScanType>     <ScanDescription>Delivered</ScanDescription>     <City>DETROIT</City>     <StateOrProvinceCode>MI</StateOrProvinceCode>     <CountryCode>US</CountryCode>   </Scan>   <Scan>     <Date>2006-01-11</Date>     <Time>07:26</Time>     <ScanType>11</ScanType>     <ScanDescription>On FedEx vehicle for delivery</ScanDescription>     <City>DETROIT</City>     <StateOrProvinceCode>MI</StateOrProvinceCode>     <CountryCode>US</CountryCode>   </Scan>   <Scan>     <Date>2006-01-11</Date>     <Time>05:49</Time>     <ScanType>02</ScanType>     <ScanDescription>Arrived at FedEx Destination Location</ScanDescription>     <City>DETROIT</City>     <StateOrProvinceCode>MI</StateOrProvinceCode>     <CountryCode>US</CountryCode>   </Scan>   <Scan>     <Date>2006-01-11</Date>     <Time>01:39</Time>     <ScanType>10</ScanType>     <ScanDescription>Left FedEx Sort Facility</ScanDescription>     <City>MEMPHIS</City>     <StateOrProvinceCode>TN</StateOrProvinceCode>     <CountryCode>US</CountryCode>   </Scan>   <Scan>     <Date>2006-01-10</Date>     <Time>15:44</Time>     <ScanType>08</ScanType>     <ScanDescription>Picked up by FedEx</ScanDescription>     <City>MEMPHIS</City>     <StateOrProvinceCode>TN</StateOrProvinceCode>     <CountryCode>US</CountryCode>   </Scan> </TrackProfile> </FDXTrackReply> 

As you can see, even with domestic shipments there can be quite a lot of scans for a single package. Parsed and nicely formatted output is much more useful:

 2006-01-11,09:53 - Delivered in DETROIT, MI 2006-01-11,07:34 - On FedEx vehicle for delivery in DETROIT, MI 2006-01-11,06:39 - Arrived at FedEx Destination Location in DETROIT, MI 2006-01-11,03:41 - Left FedEx Sort Facility in MEMPHIS, TN 2006-01-10,18:44 - Picked up by FedEx in MEMPHIS, TN 

Additionally, recognize that FedEx returns scan information with the most recent information at the top, rather than chronologically. I generally find it more convenient to reverse this, so I can read details in chronological order. The opposing viewpoint (presumably FedEx's) is that by presenting information in this order one can more quickly obtain the latest information.

The code to send the request and give the output is as follows:

 echo "<h3>Request</h3>\n"; echo "<pre>\n"; print_r(simplexml_load_string($request)); echo "</pre>\n"; echo "<h3>Response</h3>\n"; $response = callFedEx($request); if (!isset($response->TrackProfile->SoftError)) {   foreach ($response->TrackProfile->Scan AS $scanPoint)   {     echo "{$scanPoint->Date},{$scanPoint->Time} - {$scanPoint->ScanDescription} in       {$scanPoint->City}, {$scanPoint->StateOrProvinceCode}<br>";   } }else {   echo "Tracking information not yet available"; } echo "<pre>"; print_r($response); echo "</pre>"; 




Professional Web APIs with PHP. eBay, Google, PayPal, Amazon, FedEx, Plus Web Feeds
Professional Web APIs with PHP. eBay, Google, PayPal, Amazon, FedEx, Plus Web Feeds
ISBN: 764589547
EAN: N/A
Year: 2006
Pages: 130

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net