petschko/dhl-php-sdk
Unofficial DHL SOAP API PHP SDK for creating/deleting shipments and generating labels. Supports DHL SOAP API v2+ (v3 available via dev branch). Requires PHP 5.4+ and the PHP SOAP extension. Note: repository is inactive/out of support.
Installation
composer require petschko/dhl-php-sdk
Verify the package loads in composer.json under require.
First Use Case: Authentication Initialize the client with your DHL API credentials:
use DHL\Api\Client;
use DHL\Api\Authentication\BasicAuthentication;
$client = new Client();
$client->setAuthentication(new BasicAuthentication('your-username', 'your-password'));
Locate credentials in .env or a secure config file (never hardcode).
First API Call (Shipment Tracking)
$trackingResponse = $client->getShipmentTracking()->getShipmentTracking('1234567890');
dd($trackingResponse->getShipmentTrackingResponse());
Check the DHL API docs for endpoint specifics.
Common Operations
getShipmentTracking() for real-time status.createShipment() with getLabel():
$shipment = $client->getShipment()->createShipment($requestData);
$label = $client->getLabel()->getLabel($shipment->getShipmentId());
getShipmentTracking()->getShipmentTracking($trackingNumbers).Request/Response Handling
DHL\Api\Request\ShipmentRequest for structured payloads.getShipmentTrackingResponse()->getTrackingItems()).Error Handling
try {
$response = $client->getShipment()->createShipment($data);
} catch (\DHL\Api\Exception\ApiException $e) {
log::error('DHL API Error: ' . $e->getMessage());
return response()->json(['error' => 'DHL Service Unavailable'], 500);
}
$this->app->singleton(Client::class, function () {
$client = new Client();
$client->setAuthentication(new BasicAuthentication(config('dhl.username'), config('dhl.password')));
return $client;
});
Dispatch(new ProcessDhlShipments($trackingNumbers))->onQueue('dhl');
$trackingData = Cache::remember("dhl_tracking_{$trackingNumber}", now()->addHours(1), function () use ($client, $trackingNumber) {
return $client->getShipmentTracking()->getShipmentTracking($trackingNumber);
});
Deprecated API Version
Authentication Issues
$client->setAuthentication(new OAuth2Authentication($token));
use Symfony\Component\Cache\Adapter\AdapterInterface;
$cache = app(AdapterInterface::class);
if (!$cache->get("dhl_rate_limit_{$ip}", false)) {
$cache->set("dhl_rate_limit_{$ip}", true, 60); // 1-minute cooldown
$response = $client->getShipmentTracking()->getShipmentTracking($number);
}
Response Parsing Quirks
collect($response)->toArray().if (empty($response->getShipmentTrackingResponse()->getTrackingItems())) {
throw new \RuntimeException('No tracking data found');
}
$client->setLogger(new \Monolog\Logger('dhl', [
new \Monolog\Handler\StreamHandler(storage_path('logs/dhl.log'))
]));
$this->mock(DHL\Api\Client::class, function ($mock) {
$mock->shouldReceive('getShipmentTracking')
->andReturnSelf()
->shouldReceive('getShipmentTracking')
->andReturn(new \DHL\Api\Response\ShipmentTrackingResponse([...]));
});
Custom Request/Response Classes
Extend DHL\Api\Request\AbstractRequest or DHL\Api\Response\AbstractResponse to add domain-specific logic.
Middleware for Requests Add headers or modify payloads via a middleware:
$client->addMiddleware(function ($request) {
$request->setHeader('X-Custom-Header', 'value');
return $request;
});
Webhook Integration
Use DHL’s webhook API (if supported) to push updates to your app. Store callbacks in Laravel’s queue:work:
Route::post('/dhl/webhook', function (Request $request) {
dispatch(new HandleDhlWebhook($request->json()->all()));
});
How can I help you explore Laravel packages today?