PHP code example of chinphy / express-tracking

1. Go to this page and download the library: Download chinphy/express-tracking library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

chinphy / express-tracking example snippets




use GuzzleHttp\Client;
use GuzzleHttp\Psr7\HttpFactory;
use Chinphy\ExpressTracking\Carrier;
use Chinphy\ExpressTracking\ExpressTrackingClient;
use Chinphy\ExpressTracking\Provider\CNCYProvider;
use Chinphy\ExpressTracking\Provider\EMSProvider;
use Chinphy\ExpressTracking\Provider\JDProvider;
use Chinphy\ExpressTracking\Provider\KDNProvider;
use Chinphy\ExpressTracking\Provider\SFProvider;
use Chinphy\ExpressTracking\Provider\STOProvider;
use Chinphy\ExpressTracking\Provider\ZTOProvider;

$http = new HttpFactory();
$guzzle = new Client(['timeout' => 10]);

$client = new ExpressTrackingClient([
    new SFProvider('你的customerCode', '你的checkword', $guzzle, $http, $http),
    new ZTOProvider('你的AppKey', '你的AppSecret', $guzzle, $http, $http),
    new JDProvider(
        '你的AppKey',
        '你的AppSecret',
        '你的accessToken',
        '你的客户编码(010K...)',
        $guzzle,
        $http,
        $http,
    ),
    new EMSProvider('你的companyId(客户标识)', '你的queryKey(查询密钥)', $guzzle, $http, $http),
    new STOProvider('你的AppKey', '你的Secret', $guzzle, $http, $http),
    new CNCYProvider('你的AppKey', '你的AppSecret', '你的customerId', '你的ownerCode', $guzzle, $http, $http),
    new KDNProvider('你的EBusinessID', '你的ApiKey', 'CNCY', '客户名', $guzzle, $http, $http),
    // JOS 老平台通道(已在 JOS 开通的账号使用,与 LOP 新版二选一即可):
    // new JDOSProvider('你的AppKey', '你的AppSecret', '你的accessToken', '你的tradeCode(事业部编码)', $guzzle, $http, $http),
]);

// 单次查询,保留原始数据
$result = $client->track(Carrier::SF, 'SF1234567890', keepRaw: true);

echo $result->status;      // delivered / in_transit / ...
echo $result->statusText;  // 最新一条轨迹描述
foreach ($result->traces as $point) {
    printf(
        "%s | %s | %s | %s\n",
        $point->time?->format('Y-m-d H:i:s'),
        $point->location ?? '-',
        $point->status,
        $point->description
    );
}

// 原始轨迹数据(开启 keepRaw 时可用)
var_dump($result->raw);

$provider = new JDProvider(
    // ... 其他参数,
    tokenRefresher: function (string $trackingNo): string {
        return refreshJdToken(); // 自行实现 OAuth 刷新,返回新 token
    },
);

// JOS:多个事业部编码
new JDOSProvider(
    'AppKey', 'AppSecret', 'accessToken',
    ['010K916912', '028K7801302'],   // 多个 tradeCode,按序尝试
    $guzzle, $http, $http,
);

// LOP:多个客户编码
new JDProvider(
    'AppKey', 'AppSecret', 'accessToken',
    ['010K1233455', '010K916912'],   // 多个 customerCode,按序尝试
    $guzzle, $http, $http,
);

// 门面统一入口(carrier + 站点参数)
$result = $client->trackWithSite(Carrier::JDOS, 'JDV002516977833', '010K916912', keepRaw: true);

// 或直接调用 Provider
$jdos->trackWithSite('JDV002516977833', '010K916912');
$jd->trackWithSite('JDVA1234567890', '010K1233455');