Tracking FedEx and UPS Packages Online - Page 25
January 21, 2002
|
The script in this article will keep your customers on your site
and provide self-service package tracking capabilities for
Federal Express and/or UPS.
|
Overview
Both UPS and
Federal Express provide
package tracking information on their Web sites. When customers
purchase a product from your e-commerce site, it's preferable,
however, to keep them on your site instead of forcing them to go
off-site to track their package. You can provide this valuable
service to your customers on your site with a Perl script. Users
enter the order number, which is assigned by you and stored in a
comma delimited CSV file which can originate from a database or
spreadsheet. The order number cross-references the corresponding
tracking number for either Federal Express or UPS. The script
then uses the tracking number to look up the latest tracking data
from FedEx or UPS and prints the results to the user's browser.
To get this script to work on your site, put the
tracker.pl script in the cgi-bin
directory on your Web server. You will need a recent version of
Perl installed. If you are running a Windows platform, you can
download Perl from
Activestate; otherwise, you can download Perl from
CPAN. You will also need to download
tracker.csv
and place the file somewhere on your system.
In addition to Perl, you will need the
Business::UPS and
Text::CSV_XS modules, also available from CPAN. If you
are running Perl on a Windows server with
Activestate Perl, you can install the module by typing the
following commands from within a DOS prompt:
ppm install Business-UPS
ppm install Text-CSV_XS ppm
install LWP-Simple
With a regular installation of Perl, you can install the module
by typing the following on the command line:
perl -MCPAN -e 'install Business::UPS'
perl -MCPAN -e 'install Text::CSV_XS
perl -MCPAN -e 'install LWP::Simple
Throughout this article, I will be referring to the
numbered source code, so you might want to open this source
in a separate window for reference as we step through the code.
Looking up Tracking Numbers
When a customer places an order, you probably track that
customer's order with a unique number that originates from a
database or e-commerce application. The tracker.pl application
uses this number as a key in the tracker.csv file. This is the
number that the customer will enter on the package tracking form.
You will need to export this unique order number along with the
tracking number for the FedEx or UPS package. The CSV file should
contain 3 fields. The first field is the unique order number. The
second and third fields are the FedEx and UPS tracking numbers
respectively. Either the FedEx or UPS field will contain a value
depending on which carrier you used to ship the package. Below is
a sample tracker.csv file:
"123","830271123988",""
"321","","1Z 644 23W 02 4675 385 5"
The first line of the sample CSV file is order number 123. The
package was shipped via FedEx and the tracking number is
830271123988. The second order number is 321 and was shipped via
UPS; its tracking number is 1Z 644 23W 02 4675 385 5.
The location of this CSV file is set in the
$csv_file variable on line 12 of
tracker.pl. When the customer submits their order
number to the Perl script, we need to parse the CSV file and
lookup the tracking number. To accomplish this, I created the
&get_tracking_num subroutine located on lines
41-58. Line 43 opens the CSV file and line 44 creates a new
instance of the Text::CSV_XS module. This module provides the
function to easily parse almost any type of CSV file, whether it
was created by a database export or a spreadsheet program. On
line 46, we loop over each line of the program, parse the line on
line 47 with the parse() method, and grab the fields
for the record with the fields() method on line 48.
If the order number that we are searching for matches the order
number in the record on line 49, we return the FedEx (or UPS)
tracking number to the caller.
Keeping this CSV file is beyond the scope of this article and
will be maintained automatically or manually by you. This script
only reads from it to determine what the tracking number is for
the order.
Saving The New Index Image - Page 24
Weaving Magic With Regular Expressions
Tracking The Packages - Page 26
|