Tuesday, December 6, 2016

UPS Tracking API using PHP to Track status of Shipment

///////////////////////////////////////////////////////////////////////////////////////////////////
//// index.php
///////////////////////////////////////////////////////////////////////////////////////////////////
<div align="center">
<form action="#" method="post">
<table>
  <tr><td>Enter Tracking No</td><td><input type="text" id="trackno" name="trackno" /></td><td><input type="submit" value="Enter" id="sub" name="sub" /></td></tr>
</table>
</form>
</div>
<?php
include("track_detail.php");
if(isset($_REQUEST['sub'])){
 echo $trackingNumber=$_REQUEST['trackno'];
 $objTrack=new trackApi;
 $objTrack->trackingApi($trackingNumber);
}
?>
///////////////////////////////////////////////////////////////////////////////////////////////////
//// End index.php
///////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////
//// track_detail.php
///////////////////////////////////////////////////////////////////////////////////////////////////

<?php
//beginin track Api

class trackApi{
 function trackingApi($trackingNumber){
  $ch=curl_init("https://www.ups.com/ups.app/xml/Track");
  curl_setopt($ch, CURLOPT_HEADER,1);
  curl_setopt($ch, CURLOPT_POST,1);
  curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
  curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
  curl_setopt($ch, CURLOPT_POSTFIELDS,
                    "xmlRequest=" .$this->getXml($trackingNumber));
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  $res=curl_exec($ch);
  $data=strstr($res,'<?');
  $myres=simplexml_load_string($data);
  $myres->asXML("result.xml");
  header("Location:status.php");  

 }

 function getXml($trackingNumber){
 $data="<?xml version=\"1.0\"?>
        <AccessRequest xml:lang='en-US'>
                <AccessLicenseNumber>1C7D006816ADB170</AccessLicenseNumber>
                <UserId>YOUR_ID</UserId>
                <Password>YOUR_PWD</Password>
        </AccessRequest>
        <?xml version=\"1.0\"?>
        <TrackRequest>
                <Request>
<RequestOption>10</RequestOption>
                        <TransactionReference>
                                <CustomerContext>
                                        <InternalKey>KEY</InternalKey>
                                </CustomerContext>
                                <XpciVersion>1.0</XpciVersion>
                        </TransactionReference>
                        <RequestAction>Track</RequestAction>
                </Request>
        <TrackingNumber>$trackingNumber</TrackingNumber>
        </TrackRequest>";
return $data;
 }//////////////////end get XML

}/////////////END class
?>
///////////////////////////////////////////////////////////////////////////////////////////////////
//// End track_detail.php
///////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////
//// status.php
///////////////////////////////////////////////////////////////////////////////////////////////////

<?php
$xml=simplexml_load_file("result.xml");
if($xml->Response->Error->ErrorCode){
echo "Status:".$xml->Response->ResponseStatusDescription."<br/>";
echo "Error Code:".$xml->Response->Error->ErrorCode."<br/>";
echo "Error Severity:".$xml->Response->Error->ErrorSeverity."<br/>";
echo "Error ErrorDescription:".$xml->Response->Error->ErrorDescription."<br/>";


}
else{

echo "Status:".$xml->Response->ResponseStatusDescription."<br/>";
echo "Date:".$xml->Shipment->Package->Activity->Date."<br/>";
echo "Left At:".$xml->Shipment->Package->Activity->ActivityLocation->Description."<br/>";
echo "Signed For By:".$xml->Shipment->Package->Activity->ActivityLocation->SignedForByName."<br/>";
$data=$xml->Shipment->Package->Activity->ActivityLocation->SignatureImage->GraphicImage;
echo'<img src="data:image/GIF;base64,'.$data.'">';
}
?>
///////////////////////////////////////////////////////////////////////////////////////////////////
//// End status.php
///////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////
//// result.xml
///////////////////////////////////////////////////////////////////////////////////////////////////
<?xml version="1.0"?>
<TrackResponse><Response><TransactionReference><XpciVersion>1.0</XpciVersion></TransactionReference><ResponseStatusCode>1</ResponseStatusCode><ResponseStatusDescription>Success</ResponseStatusDescription></Response><Shipment><Shipper><ShipperNumber>7X987R</ShipperNumber><Address><AddressLine1>7729 LOCHLLN DR</AddressLine1><City>BRIGHTON</City><StateProvinceCode>MI</StateProvinceCode><PostalCode>48116   8329</PostalCode><CountryCode>US</CountryCode></Address></Shipper><ShipTo><Address><AddressLine1>11 HULSE RD</AddressLine1><City>SETAUKET</City><StateProvinceCode>NY</StateProvinceCode><PostalCode>117333617</PostalCode><CountryCode>US</CountryCode></Address></ShipTo><Service><Code>003</Code><Description>GROUND</Description></Service><ReferenceNumber><Code>01</Code><Value>ES230821</Value></ReferenceNumber><ShipmentIdentificationNumber>1Z7X887R0354471707</ShipmentIdentificationNumber><PickupDate>20141114</PickupDate><DeliveryDateUnavailable><Type>Scheduled Delivery</Type><Description>Scheduled Delivery Date is not currently available, please try back later</Description></DeliveryDateUnavailable><Package><TrackingNumber>1Z7X887R03754471707</TrackingNumber><Activity><ActivityLocation><Address><AddressLine1>11 HULSE RD</AddressLine1><City>EAST SETAUKET</City><StateProvinceCode>NY</StateProvinceCode><PostalCode>11733</PostalCode><CountryCode>US</CountryCode></Address><Code>AI</Code><Description>DOCK</Description><SignedForByName>AYERLE</SignedForByName><SignatureImage><GraphicImage>CxhwfZyH35h1HCXBaL28yiSEHsnEO8ow8RUAAAD.....</GraphicImage><ImageFormat><Code>GIF</Code><Description>Base 64 encoded gif</Description></ImageFormat></SignatureImage><PODLetter><HTMLImage>9EWT48L0hUTU...</HTMLImage></PODLetter></ActivityLocation><Status><StatusType><Code>D</Code><Description>DELIVERED</Description></StatusType><StatusCode><Code>KB</Code></StatusCode></Status><Date>20141118</Date><Time>115000</Time></Activity><PackageWeight><UnitOfMeasurement><Code>LBS</Code></UnitOfMeasurement><Weight>18.00</Weight></PackageWeight><ReferenceNumber><Code>01</Code><Value>FR30821</Value></ReferenceNumber></Package></Shipment></TrackResponse>

///////////////////////////////////////////////////////////////////////////////////////////////////
//// End result.xml
///////////////////////////////////////////////////////////////////////////////////////////////////


No comments:

Post a Comment