slince / shipment-tracking

:sparkles: A flexible shipment tracking library for multi carriers like DHL eCommerce, USPS, YanWen Express, Epacket, E包裹, E特快, 国际EMS, 快递100,递四方 etc.
http://mypackage.co/tracking?orderNo=9037866297519&trackingNum=LW489083733CN
29 stars 9 forks source link
china-post dhl-ecommerce epacket parcel shipment shipment-tracker tracking usps

Shipment Tracking SDK

Build Status Coverage Status Latest Stable Version Scrutinizer

A flexible and awesome shipment tracking library for several carriers like DHL eCommerce, YanWen Express, Epacket, E包裹, E特快, 国际EMS, 快递100

Installation

Install via composer

$ composer require slince/shipment-tracking

Table of Contents

Basic Usage

DHL eCommerce


$tracker = new Slince\ShipmentTracking\DHLECommerce\DHLECommerceTracker(CLIENT_ID, PASSWORD);

try {
   $shipment = $tracker->track('CNAQV100168101');

   if ($shipment->isDelivered()) {
       echo "Delivered";
   }
   echo $shipment->getOrigin();
   echo $shipment->getDestination();
   print_r($shipment->getEvents());  //print the shipment events

} catch (Slince\ShipmentTracking\Foundation\Exception\TrackException $exception) {
    exit('Track error: ' . $exception->getMessage());
}

The above code will get access token automatically for shipment information.

Access Token

$shipment = $tacker->track('CNAQV100168101);
$accessToken = $tracker->getAccessToken(); //You can save this for the next query

//... to do

try{
    $tracker->setAccessToken($accessToken); //Set the access token; the tracker will not send requst for the access token
    $shipment = $tacker->track('CNAQV100168101);
} catch (Slince\ShipmentTracking\DHLECommerce\Exception\InvalidAccessTokenException $exception) {
     $accessToken = $tracker->getAccessToken(true); // If the access token is invalid, refresh it.
     $shipment = $tacker->track('CNAQV100168101);
     //... to do
} catch (Slince\ShipmentTracking\Foundation\Exception\TrackException $exception) {
    exit('Track error: ' . $exception->getMessage());
}

YanWen Express(燕文物流)


$tracker = new Slince\ShipmentTracking\YanWenExpress\YanWenTracker(KEY, 'en');

try {
   $shipment = $tracker->track('CNAQV100168101');

   if ($shipment->isDelivered()) {
       echo "Delivered";
   }
   echo $shipment->getOrigin();
   echo $shipment->getDestination();
   print_r($shipment->getEvents());  //print the shipment events

} catch (Slince\ShipmentTracking\Foundation\Exception\TrackException $exception) {
    exit('Track error: ' . $exception->getMessage());
}

中国邮政

适用中邮旗下E邮宝(Epacket、EUP)、E包裹、E特快、国际EMS产品


$tracker = new Slince\ShipmentTracking\EMS\EMSTracker(AUTHENTICATE, 'en');

try {
   $shipment = $tracker->track('CNAQV100168101');

   print_r($shipment->getEvents());  //print the shipment events

} catch (Slince\ShipmentTracking\Foundation\Exception\TrackException $exception) {
    exit('Track error: ' . $exception->getMessage());
}

注意:该库并不支持查询国内EMS包裹信息,相关文档主页http://shipping.ems.com.cn/apiIndex

快递100


$tracker = new Slince\ShipmentTracking\KuaiDi100\KuaiDi100Tracker(APPKEY, 'shunfeng'); //承运商名称并不是标准的承运商代码,实际承运商代码请到kuaidi100.com查看

try {
   $shipment = $tracker->track('CNAQV100168101');

   if ($shipment->isDelivered()) {
       echo "Delivered";
   }
   print_r($shipment->getEvents());  //print the shipment events

} catch (Slince\ShipmentTracking\Foundation\Exception\TrackException $exception) {
    exit('Track error: ' . $exception->getMessage());
}

快递100的key需要自行申请,免费版的key在查询申通顺丰之类的单号时会受限,需要企业版才可以;附上快递100文档

USPS


$tracker = new Slince\ShipmentTracking\USPS\USPSTracker(USER_ID);

try {
   $shipment = $tracker->track('CNAQV100168101');

   if ($shipment->isDelivered()) {
       echo "Delivered";
   }
   print_r($shipment->getEvents());  //print the shipment events

} catch (Slince\ShipmentTracking\Foundation\Exception\TrackException $exception) {
    exit('Track error: ' . $exception->getMessage());
}

You can get your user id on the following url.

https://www.usps.com/business/web-tools-apis/welcome.htm

递四方


$tracker = new Slince\ShipmentTracking\FourPartyExpress\FourPartyExpressTracker(APPKEY, APPSECRET);

try {
   $shipment = $tracker->track('CNAQV100168101');

   print_r($shipment->getEvents());  //print the shipment events

} catch (Slince\ShipmentTracking\Foundation\Exception\TrackException $exception) {
    exit('Track error: ' . $exception->getMessage());
}

APPKEY和APPSECRET 需要到递四方官网注册APP,审核之后即可获取到该参数;

License

The MIT license. See MIT