Commerce
|
|
|
|
<?
error_reporting(7);
/**
* UPSTrack Class
*
* This class enables a website to get real time tracking results from ups.
*
* @version 1.0
* @author Jon Whitcraft <jon_whitcraft@sweetwater.com>
*
*/ class upsTrack {
//////////////////////////
// Public Variables //
//////////////////////////
/**
* userid
*
* This is your ups userid. It is needed to connect to the ups servers.
*
* @access public
* @see __runCurl()
*/
var $userid = "XXXXXXX";
/**
* passwd
*
* This is your ups password. It is needed to connect to the ups servers.
*
* @access public
* @see __runCurl()
*/
var $passwd = "XXXXXXX";
/**
* accesskey
*
* This is your ups accesskey. It is needed to connect to the ups servers.
* UPS will give you this when you sign up on their website.
*
* @access public
* @see __runCurl()
*/
var $accesskey = "XXXXXXX";
/**
* dateformat
*
* This is used in the format_date() function. You can use it to format the date(s) that ups sends back in the xml.
*
* @access public
* @see format_date()
*/
var $dateformat = "m/d/y";
/**
* accesskey
*
* This is used in the format_time() function. You can use it to format the time(s) that ups send back in the xml.
*
* @access public
* @see format_time()
*/
var $timeformat = "h:i a";
/**
* xmlarray
*
* This variable is used to store the array that comes out of the xml.
* It is set when the _get_xml_array() is called.
*
* @access public
* @see processTrack(),_get_xml_array()
*/
var $xmlarray = array();
///////////////////////////
// Private Variables //
///////////////////////////
/**
* trackingnumber
*
* Where the tracking number is stored in the class.
* This variable is set when processTrack() is called.
*
* @access private
* @see processTrack(),$__runCurl
*/
var $trackingnumber = "";
/**
* xmlreturndata
*
* This variable is used to store the xml that comes back from ups.
*
* @access private
* @see __runCurl()
*/
var $xmlreturndata = "";
///////////////////////////
// Functions //
///////////////////////////
/**
* upsTrack
*
* This is the init function for the class.
* It is called every time a new class is instantiated.
*
* @access public
*/
function upsTrack()
{
// init function
}
/**
* processTrack($tracknum)
*
* This is the init function for the class.
* It is called every time a new class is instantiated.
*
* @access public
* @param string $tracknum The tracking number that you would like to track
*/
function processTrack($tracknum)
{
$this->trackingnumber = $tracknum;
$this->__runCurl();
$this->xmlarray = $this->_get_xml_array($this->xmlreturndata);
//$this->xmlarray = $this->xml($this->xmlreturndata,"xml");
}
/**
* __runCurl()
*
* This is processes the curl command.
*
* @access private
*/
function __runCurl()
{
$y = "<?xml version="1.0"?><AccessRequest xml:lang="en-US"><AccessLicenseNumber>".$this->accesskey."</AccessLicenseNumber>";
$y .= "<UserId>".$this->userid."</UserId><Password>".$this->passwd."</Password></AccessRequest><?xml version="1.0"?><TrackRequest xml:lang="en-US">";
$y .= "<Request><TransactionReference><CustomerContext>Example 1</CustomerContext><XpciVersion>1.0001</XpciVersion></TransactionReference>";
$y .= "<RequestAction>Track</RequestAction><RequestOption>activity</RequestOption></Request><TrackingNumber>".$this->trackingnumber."</TrackingNumber></TrackRequest>";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL,"https://www.ups.com/ups.app/xml/Track"); /// set the post-to url (do not include the ?query+string here!)
curl_setopt ($ch, CURLOPT_HEADER, 0); /// Header control
curl_setopt ($ch, CURLOPT_POST, 1); /// tell it to make a POST, not a GET
curl_setopt ($ch, CURLOPT_POSTFIELDS, "$y"); /// put the query string here starting with "?"
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); /// This allows the output to be set into a variable $xyz
$this->xmlreturndata = curl_exec ($ch); /// execute the curl session and return the output to a variable $xyz
curl_close ($ch); /// close the curl session
}
/**
* __get_xml_array($values, &$i)
*
* This is adds the contents of the return xml into the array for easier processing.
*
* @access private
* @param array $values this is the xml data in an array
* @param int $i this is the current location in the array
* @return Array
*/
function __get_xml_array($values, &$i)
{
$child = array();
if ($values[$i]['value']) array_push($child, $values[$i]['value']);
while (++$i < count($values))
{
switch ($values[$i]['type'])
{
case 'cdata':
array_push($child, $values[$i]['value']);
break;
case 'complete':
$name = $values[$i]['tag'];
$child[$name]= $values[$i]['value'];
if($values[$i]['attributes'])
{
$child[$name] = $values[$i]['attributes'];
}
break;
case 'open':
$name = $values[$i]['tag'];
$size = sizeof($child[$name]);
if($values[$i]['attributes'])
{
$child[$name][$size] = $values[$i]['attributes'];
$child[$name][$size] = $this->__get_xml_array($values, $i);
}
else
{
$child[$name][$size] = $this->__get_xml_array($values, $i);
}
break;
case 'close':
return $child;
break;
}
}
return $child;
}
/**
* _get_xml_array($data)
*
* This is adds the contents of the return xml into the array for easier processing.
*
* @access private
* @param string $data this is the string of the xml data
* @return Array
*/
function _get_xml_array($data)
{
$values = array();
$index = array();
$array = array();
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parse_into_struct($parser, $data, $values, $index);
xml_parser_free($parser);
$i = 0;
$name = $values[$i]['tag'];
$array[$name] = $values[$i]['attributes'];
$array[$name] = $this->__get_xml_array($values, $i);
return $array;
}
/**
* get_general_package_info()
*
* This function parses the big array and returns the general info about the shiped package.
*
* @access public
* @return Array
*/
function get_general_package_info()
{
$retArray = array("Service"=>"","PickupDate"=>"","ScheduledDeliveryDate"=>"","DeliveryTime"=>"","SignedForByName"=>"","Description"=>"");
$values = $this->xmlarray['TrackResponse']['Shipment'][0];
$retArray['Service'] = $values['Service'][0];
$retArray['PickupDate'] = $values['PickupDate'];
$retArray['ScheduledDeliveryDate'] = $values['ScheduledDeliveryDate'];
$retArray['SignedForByName'] = $values['Package'][0]['Activity'][0]['ActivityLocation'][0]['SignedForByName'];
$retArray['DeliveryTime'] = $values['Package'][0]['Activity'][0]['Time'];
$retArray['Description'] = $values['Package'][0]['Activity'][0]['ActivityLocation'][0]['Description'];
unset($values);
return $retArray;
}
/**
* get_shipper_info()
*
* This function parses the big array and returns the shipper info about the shiped package.
*
* @access public
* @return Array
*/
function get_shipper_info()
{
$retArray = array("ShipperNumber"=>"","Address"=>"");
$values = $this->xmlarray['TrackResponse']['Shipment'][0]['Shipper'][0];
$retArray['ShipperNumber'] = $values['ShipperNumber'];
$retArray['Address'] = $values['Address'][0];
unset($values);
return $retArray;
}
/**
* get_shipto_info()
*
* This function parses the big array and returns the ship-to info for the package.
*
* @access public
* @return Array
*/
function get_shipto_info()
{
$retArray = array("Address"=>"");
$values = $this->xmlarray['TrackResponse']['Shipment'][0]['ShipTo'][0];
$retArray['Address'] = $values['Address'][0];
unset($values);
return $retArray;
}
/**
* get_package_info()
*
* This function parses the big array and returns the package activity.
*
* @access public
* @return Array
*/
function get_package_info()
{
$retArray = array("Activity"=>"","PackageWeight"=>"");
$tmpArray = array();
$values = $this->xmlarray['TrackResponse']['Shipment'][0]['Package'][0];
//count($values['Activity']);
for($x=0;$x<count($values['Activity']);$x++)
{
$arrTmp = Array("ActivityLocation"=>"","Status"=>"","Date"=>"","Time"=>"");
$arrTmpAL = Array("Address"=>"","Code"=>"","Description"=>"","SignedForByName"=>"");
$arrTmpAL['Address'] = $values['Activity'][$x]['ActivityLocation'][0]['Address'][0];
$arrTmpAL['Code'] = $values['Activity'][$x]['ActivityLocation'][0]['Code'];
$arrTmpAL['Description'] = $values['Activity'][$x]['ActivityLocation'][0]['Description'];
$arrTmpAL['SignedForByName'] = $values['Activity'][$x]['ActivityLocation'][0]['SignedForByName'];
$arrTmp['ActivityLocation'] = $arrTmpAL;
unset($arrTmpAL);
$arrTmpST = array("StatusType"=>"","StatusCode"=>"");
$arrTmpST['StatusType'] = $values['Activity'][$x]['Status'][0]['StatusType'][0];
$arrTmpST['StatusCode'] = $values['Activity'][$x]['Status'][0]['StatusCode'][0];
$arrTmp['Status'] = $arrTmpST;
//unset($arrTmpST);
$arrTmp['Date'] = $values['Activity'][$x]['Date'];
$arrTmp['Time'] = $values['Activity'][$x]['Time'];
array_push($tmpArray,$arrTmp);
unset($arrTmp);
}
$arrTmpPW = Array("UnitOfMeasurement"=>"","Weight"=>"");
$arrTmpPW['UnitOfMeasurement'] = $values['PackageWeight'][0]['UnitOfMeasurement'][0];
$arrTmpPW['Weight'] = $values['PackageWeight'][0]['Weight'];
$retArray['PackageWeight'] = $arrTmpPW;
unset($arrTmpPW);
$retArray['Activity'] = $tmpArray;
unset($tmpArray);
return $retArray;
}
/**
* format_date()
*
* This formats the date from YYYYMMDD to what ever you specify in $dateformat.
*
* @access public
* @param string $data this is the YYYYMMDD that you want formated.
* @return String
*/
function format_date($data)
{
$temp = date($this->dateformat, mktime(0,0,0,substr($data, 4, 2),substr($data, 6, 2),substr($data, 0, 4)));
return $temp;
}
/**
* format_time()
*
* This formats the date from HHMMSS to what ever you specify in $timeformat.
*
* @access public
* @param string $data this is the HHMMSS that you want formated.
* @return String
*/
function format_time($data)
{
$temp = date($this->timeformat, mktime(substr($data, 0, 2),substr($data, 2, 2),substr($data, 4, 2),date("m"),date("d"),date("Y")));
return $temp;
}
} ?>
|
|
|
Usage Example
|
$ups = new upsTrack();
$ups->processTrack("TrackingNumber");
$data = $ups->xmlarray;
|
|
|
Rate This Script
|
|
|
|