PHP code example of tcgunel / omniship-dhl-express

1. Go to this page and download the library: Download tcgunel/omniship-dhl-express 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/ */

    

tcgunel / omniship-dhl-express example snippets


use Omniship\Omniship;
use Omniship\Common\Address;
use Omniship\Common\Package;

$dhl = Omniship::create('DHL_Express');
$dhl->initialize([
    'username' => 'your-api-key',       // MyDHL API Key
    'password' => 'your-api-secret',    // MyDHL API Secret
    'accountNumber' => '123456789',     // DHL account number (9-10 digits)
    'testMode' => true,
]);

$response = $dhl->createShipment([
    'shipFrom' => new Address(
        name: 'Ahmet Yilmaz',
        company: 'Sender Company',
        street1: 'Ataturk Cad. No:42',
        city: 'Istanbul',
        district: 'Kadikoy',
        postalCode: '34710',
        country: 'TR',
        phone: '+905551234567',
        email: '[email protected]',
        taxId: 'TR1234567890',
    ),
    'shipTo' => new Address(
        name: 'John Smith',
        company: 'Receiver Inc',
        street1: '123 Main Street',
        street2: 'Suite 100',
        city: 'New York',
        state: 'NY',
        postalCode: '10001',
        country: 'US',
        phone: '+12125551234',
        email: '[email protected]',
    ),
    'packages' => [
        new Package(weight: 2.5, length: 30, width: 20, height: 15, description: 'Electronics'),
    ],
    'productCode' => 'P',                  // Express Worldwide (default)
    'plannedShippingDate' => '2026-03-13T10:00:00GMT+03:00',
    // 'pickupRequested' => false,          // default
    // 'isCustomsDeclarable' => true,       // auto-detected from countries
    // 'declaredValue' => 150.00,
    // 'declaredValueCurrency' => 'USD',
    // 'labelFormat' => 'pdf',             // pdf, zpl, epl (default: pdf)
])->send();

if ($response->isSuccessful()) {
    echo $response->getTrackingNumber();                // "1234567890"
    echo $response->getDispatchConfirmationNumber();    // needed for cancellation
    echo $response->getTrackingUrl();                   // DHL tracking page URL
    echo $response->getEstimatedDeliveryDate();         // "2026-03-16"

    $label = $response->getLabel();
    if ($label !== null) {
        file_put_contents('label.pdf', base64_decode($label->content));
    }
}

$response = $dhl->getTrackingStatus([
    'trackingNumber' => '1234567890',
])->send();

if ($response->isSuccessful()) {
    $info = $response->getTrackingInfo();
    echo $info->status->value;           // "delivered", "in_transit", etc.
    echo $info->trackingNumber;          // AWB number
    echo $info->carrier;                 // "DHL Express"
    echo $info->serviceName;             // "Express Worldwide"
    echo $info->signedBy;                // "J.SMITH" (if delivered)

    foreach ($info->events as $event) {
        echo $event->description;        // "Arrived at DHL Sort Facility"
        echo $event->city;               // "NEW YORK"
        echo $event->country;            // "USA"
        echo $event->occurredAt->format('Y-m-d H:i');
    }
}

$response = $dhl->cancelShipment([
    'trackingNumber' => '1234567890',
    'dispatchConfirmationNumber' => 'DHL-DC-123456',
    // 'requestorName' => 'Ahmet Yilmaz',
    // 'cancelReason' => '006',  // default: '008' (Other)
])->send();

if ($response->isCancelled()) {
    echo 'Pickup cancelled';
}

$response = $dhl->getRates([
    'shipFrom' => new Address(
        city: 'Istanbul',
        postalCode: '34710',
        country: 'TR',
    ),
    'shipTo' => new Address(
        city: 'New York',
        postalCode: '10001',
        country: 'US',
    ),
    'packages' => [
        new Package(weight: 2.5, length: 30, width: 20, height: 15),
    ],
    'plannedShippingDate' => '2026-03-13',
    'isCustomsDeclarable' => true,
    // 'productCode' => 'P',    // omit to get all available products
])->send();

if ($response->isSuccessful()) {
    foreach ($response->getRates() as $rate) {
        echo $rate['productCode'];       // "P"
        echo $rate['productName'];       // "EXPRESS WORLDWIDE"
        echo $rate['price'];             // 85.50 (billing currency)
        echo $rate['currency'];          // "USD"
        echo $rate['transitDays'];       // 3
        echo $rate['estimatedDelivery']; // "2026-03-16T23:59:00"
    }
}