1. Go to this page and download the library: Download kalimeromk/postal-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/ */
use KalimeroMK\PostalTracking\Services\PostalTrackingService;
$service = new PostalTrackingService(
timeout: 30, // API timeout in seconds
retryAttempts: 3, // Number of retry attempts
transformData: true // Transform API response data
);
$result = $service->trackShipment('CQ117742716DE');
use KalimeroMK\PostalTracking\Laravel\Facades\PostalTracking;
// Track a shipment
$result = PostalTracking::trackShipment('CQ117742716DE');
// With options
$result = PostalTracking::trackShipment('CQ117742716DE', [
'timeout' => 30,
'retry_attempts' => 3,
'transform' => true
]);
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use KalimeroMK\PostalTracking\Laravel\Facades\PostalTracking;
class TrackingController extends Controller
{
public function track(Request $request)
{
try {
$trackingCode = $request->get('tracking_code');
$result = PostalTracking::trackShipment($trackingCode);
return response()->json($result);
} catch (\Exception $e) {
return response()->json([
'success' => false,
'error' => $e->getMessage()
], 422);
}
}
}
use KalimeroMK\PostalTracking\Services\PostalTrackingService;
// In your controller
$service = new PostalTrackingService();
$trackingData = $service->trackShipment('CQ117742716DE');
// Return as JSON
Yii::$app->response->format = Response::FORMAT_JSON;
return $trackingData;
namespace app\controllers;
use yii\web\Controller;
use yii\web\Response;
use KalimeroMK\PostalTracking\Services\PostalTrackingService;
class TrackingController extends Controller
{
public function actionTrack()
{
Yii::$app->response->format = Response::FORMAT_JSON;
try {
$trackingCode = Yii::$app->request->get('tracking_code');
$service = new PostalTrackingService();
$result = $service->trackShipment($trackingCode);
return $result;
} catch (\Exception $e) {
Yii::$app->response->statusCode = 422;
return [
'success' => false,
'error' => $e->getMessage()
];
}
}
}
public function __construct(
int $timeout = 30,
int $retryAttempts = 3,
bool $transformData = true
)
public function trackShipment(string $trackingCode, array $options = []): array
// Getter methods
public function getTimeout(): int
public function getRetryAttempts(): int
public function getTransformData(): bool
// Setter methods (fluent interface)
public function setTimeout(int $timeout): self
public function setRetryAttempts(int $retryAttempts): self
public function setTransformData(bool $transformData): self
use KalimeroMK\PostalTracking\Services\PostalTrackingService;
// Create service with custom configuration
$service = new PostalTrackingService(
timeout: 60, // 60 seconds timeout
retryAttempts: 5, // 5 retry attempts
transformData: true // Enable data transformation
);
// Or configure after instantiation
$service = new PostalTrackingService();
$service->setTimeout(60)
->setRetryAttempts(5)
->setTransformData(true);
use PHPUnit\Framework\TestCase;
use KalimeroMK\PostalTracking\Services\PostalTrackingService;
class PostalTrackingServiceTest extends TestCase
{
public function testTrackShipmentWithValidCode()
{
$service = new PostalTrackingService();
$result = $service->trackShipment('CQ117742716DE');
$this->assertTrue($result['success']);
$this->assertArrayHasKey('data', $result);
$this->assertArrayHasKey('metadata', $result);
}
public function testTrackShipmentWithInvalidCode()
{
$this->expectException(InvalidTrackingCodeException::class);
$service = new PostalTrackingService();
$service->trackShipment('INVALID_CODE');
}
}