PHP code example of 4port / ocean-tracking
1. Go to this page and download the library: Download 4port/ocean-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/ */
4port / ocean-tracking example snippets
return [
// 使用 .env 中的配置自动初始化
'baseUrl' => env('OCEAN_TRACKING_BASE_URL', 'https://api.example.com'),
'app_id' => env('OCEAN_TRACKING_COMPANYID','0000'),
'secret' => env('OCEAN_TRACKING_SECRET','YOUR-SECRET'),
];
use Tracking\Ocean\Request\SubscribeRequest;
use Tracking\Ocean\Traits\UpdateOceanTrackingTrait;
try {
$request = new SubscribeRequest;
$body = [
'carrierCode' => 'HMM',
'billNo' => 'NBOZ0Y012345',
'portCode' => 'CNNGB',
'dataType' => [
"PORT",
"CARRIER"
]
];
$res = $request->setBody($body)->fetch();
// 或者简易订阅(只获取海运数据)
// $res = $request -> setTracking('HMM','NBOZ0Y012345')->fetch();
// 调试响应数据
// var_dump($res->getData());
// 使用 UpdateOceanTrackingTrait 将响应数据写入数据库
$tracking = $this->updateSubscribeData($res->getData());
dump($tracking->toArray());
} catch (\Throwable $th) {
var_dump($th->getMessage());
}
use Tracking\Ocean\Request\GetTrackingRequest;
use Tracking\Ocean\Models\OceanTracking;
try {
$subscriptionId = OceanTracking::firstOrFail()->subscription_id;
$request = new GetTrackingRequest();
$request->setSubscriptionId($subscriptionId);
$res = $request->fetch();
// 自动更新主表、航程、箱信息及状态节点
$tk = $this->updateOceanTracking($res->getData());
dump($tk->toArray());
} catch (\Throwable $th) {
$this->error($th->getMessage());
}
use Tracking\Ocean\Traits\UpdateOceanTrackingTrait;
$this->updateOceanTracking($res->getData());
bash
php artisan vendor:publish --tag=ocean-tracking-config
bash
php artisan migrate
bash
php artisan vendor:publish --tag=ocean-tracking-migrations