PHP code example of erikwang2013 / global-logistics
1. Go to this page and download the library: Download erikwang2013/global-logistics 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/ */
erikwang2013 / global-logistics example snippets
use GlobalLogistics\Logistics;
Logistics::configure([
// 国内
'sf' => ['partner_id' => '...', 'checkword' => '...'],
'zto' => ['company_id' => '...', 'secret' => '...'],
'yto' => ['app_key' => '...', 'app_secret' => '...'],
'jt' => ['api_key' => '...', 'secret' => '...'],
'yd' => ['app_key' => '...', 'app_secret' => '...'],
'sto' => [],
'jd' => [],
'ems' => ['app_id' => '...'],
'ht' => ['partner_id' => '...', 'token' => '...'],
'debon' => ['app_key' => '...', 'app_secret' => '...'],
'ky' => ['app_key' => '...', 'app_secret' => '...'],
'ane' => ['app_key' => '...'],
// 国际
'dhl' => ['client_id' => '...', 'client_secret' => '...'],
'fedex' => ['client_id' => '...', 'client_secret' => '...'],
'ups' => ['client_id' => '...', 'client_secret' => '...'],
'usps' => ['user_id' => '...'],
'royal-mail' => ['client_id' => '...', 'client_secret' => '...'],
'canada-post' => ['customer_number' => '...', 'api_key' => '...'],
'australia-post' => ['api_key' => '...'],
'japan-post' => [],
'aramex' => ['user_name' => '...', 'password' => '...', 'account_number' => '...'],
'gls' => ['api_key' => '...'],
'dpd' => ['user_name' => '...', 'password' => '...'],
'postnl' => ['api_key' => '...'],
// 可选:自定义 PSR-18 HTTP 客户端(默认自动构建 Guzzle)
'http_client' => null,
// 可选:失败重试次数(默认 2)
'max_retries' => 2,
]);
Logistics::configure([
'sf' => [
'partner_id' => getenv('SF_PARTNER_ID'),
'checkword' => getenv('SF_CHECKWORD'),
],
]);
// 自动识别通道(国内/国际)与承运商
$tracking = Logistics::track('SF1234567890');
// 显式指定(单号规则无法覆盖时)
$tracking = Logistics::domestic('sf')->queryTrack('SF1234567890');
$tracking = Logistics::international('dhl')->queryTrack('DHL1234567890');
echo $tracking->status->name; // DELIVERED
echo $tracking->latestDescription; // 快件已签收
echo $tracking->carrierCode; // sf
echo $tracking->deliveredAt?->format('Y-m-d H:i:s'); // 签收时间(已签收时)
foreach ($tracking->events as $event) {
echo $event->occurredAt?->format('Y-m-d H:i:s'), ' ', $event->location, ' ', $event->description, PHP_EOL;
}
use GlobalLogistics\Exceptions\LogisticsException;
try {
$tracking = Logistics::track('SF1234567890');
} catch (LogisticsException $e) {
// 记录 $e->getMessage(),其中含承运商代码与原始错误码,如 "[SF A1001] 必传参数不可为空"
}
use GlobalLogistics\Logistics;
$carrier = Logistics::domestic('sf');
if (!$carrier->verifyCallbackSignature((string) file_get_contents('php://input'), (string) $_SERVER['HTTP_DIGEST'])) {
http_response_code(401);
exit('signature mismatch');
}
// 验签通过,处理轨迹推送……
return [
'erikwang2013/global-logistics' => [
'sf' => ['partner_id' => '...', 'checkword' => '...'],
'dhl' => ['client_id' => '...', 'client_secret' => '...'],
],
];