PHP code example of smart-dato / dpd-sdk

1. Go to this page and download the library: Download smart-dato/dpd-sdk 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/ */

    

smart-dato / dpd-sdk example snippets


use SmartDato\Dpd\Facades\Dpd;

$shipment = Dpd::shipment()
    ->sendingDepot('0000')
    ->sender(fn($sender) => $sender
        ->name('John Doe')
        ->company('Acme Corp')
        ->street('Main Street')
        ->houseNumber('123')
        ->zipCode('12345')
        ->city('Berlin')
        ->country('DE')
    )
    ->recipient(fn($recipient) => $recipient
        ->name('Jane Smith')
        ->street('Second Avenue')
        ->houseNumber('456')
        ->zipCode('54321')
        ->city('Hamburg')
        ->country('DE')
        ->email('[email protected]')
        ->phone('+49123456789')
    )
    ->parcel(fn($parcel) => $parcel
        ->weight(2.5)
        ->content('Books')
        ->reference('ORDER-12345')
    )
    ->labelFormat('PDF')
    ->create();

// Access response data
echo "Parcel Number: {$shipment->parcelNumber}\n";
echo "MPS ID: {$shipment->mpsId}\n";
echo "Tracking URL: {$shipment->trackingUrl}\n";

// Save label to file
file_put_contents('label.pdf', $shipment->label->content);

use SmartDato\Dpd\Dpd;

// Tenant A with their own credentials
$dpdA = new Dpd([
    'environment' => 'production',
    'credentials' => [
        'delis_id' => $tenantA->dpd_delis_id,
        'password' => $tenantA->dpd_password,
    ],
]);

$shipment = $dpdA->shipment()
    ->sendingDepot('0000')
    ->sender(/* ... */)
    ->recipient(/* ... */)
    ->parcel(/* ... */)
    ->create();

// Tenant B with different credentials
$dpdB = new Dpd([
    'credentials' => [
        'delis_id' => $tenantB->dpd_delis_id,
        'password' => $tenantB->dpd_password,
    ],
]);

use SmartDato\Dpd\Facades\Dpd;

$events = Dpd::track('1234567890');

foreach ($events as $event) {
    echo "{$event->timestamp->format('Y-m-d H:i:s')} - {$event->status} at {$event->location}\n";
}

$shipment = Dpd::shipment()
    ->sendingDepot('0000')
    ->sender(/* ... */)
    ->recipient(/* ... */)
    ->parcel(fn($parcel) => $parcel
        ->weight(2.5)
        ->content('Books')
        ->reference('BOX-1')
    )
    ->parcel(fn($parcel) => $parcel
        ->weight(3.0)
        ->content('Electronics')
        ->reference('BOX-2')
    )
    ->create();

// PDF Label (A4)
$shipment = Dpd::shipment()
    // ...
    ->labelFormat('PDF')
    ->paperFormat('A4')
    ->create();

// ZPL Label for thermal printers (barcode is automatically extracted)
$shipment = Dpd::shipment()
    // ...
    ->labelFormat('ZPL')
    ->create();

// Access barcode from ZPL label
echo "Barcode: {$shipment->label->barcode}\n"; // Only available for ZPL labels
file_put_contents('label.zpl', $shipment->label->content);

$shipment = Dpd::shipment()
    ->sendingDepot('0000')
    ->mpsId('MPS-ORDER-12345') // Multi Parcel Shipment ID to group shipments
    ->customerReferenceNumber1('ORDER-12345') // e.g., Order number
    ->customerReferenceNumber2('CUSTOMER-98765') // e.g., Customer ID
    ->customerReferenceNumber3('WAREHOUSE-A') // e.g., Warehouse location
    ->customerReferenceNumber4('BATCH-001') // e.g., Batch number
    ->sender(/* ... */)
    ->recipient(/* ... */)
    ->parcel(/* ... */)
    ->create();

// The MPS ID is returned in the response
echo "MPS ID: {$shipment->mpsId}\n";

return [
    // Environment: 'staging' or 'production'
    'environment' => env('DPD_ENVIRONMENT', 'staging'),

    // DPD API Credentials
    'credentials' => [
        'delis_id' => env('DPD_DELIS_ID'),
        'password' => env('DPD_PASSWORD'),
    ],

    // Authentication Token Caching (24 hours)
    'cache' => [
        'store' => env('DPD_CACHE_STORE', null), // null = default
        'prefix' => 'dpd_auth',
        'ttl' => 86400,
    ],

    // SOAP Client Options
    'soap' => [
        'trace' => env('DPD_SOAP_TRACE', true),
        'connection_timeout' => 30,
        // ...
    ],

    // Rate Limits
    'rate_limits' => [
        'labels_per_minute' => 30,
        'calls_per_minute' => 60,
    ],

    // Default Label Options
    'defaults' => [
        'label_format' => 'PDF',
        'print_options' => [
            'printer_language' => 'PDF',
            'paper_format' => 'A4',
        ],
    ],

    // Logging for Debugging
    'logging' => [
        'enabled' => env('DPD_LOGGING_ENABLED', false),
        'channel' => env('DPD_LOGGING_CHANNEL', 'stack'),
    ],
];

use SmartDato\Dpd\Facades\Dpd;
use SmartDato\Dpd\Exceptions\AuthenticationException;
use SmartDato\Dpd\Exceptions\RateLimitException;
use SmartDato\Dpd\Exceptions\ValidationException;
use SmartDato\Dpd\Exceptions\SoapException;

try {
    $shipment = Dpd::shipment()
        ->sender(/* ... */)
        ->recipient(/* ... */)
        ->parcel(/* ... */)
        ->create();
} catch (AuthenticationException $e) {
    // Invalid credentials
    logger()->error('DPD authentication failed', ['error' => $e->getMessage()]);
} catch (RateLimitException $e) {
    // Too many requests
    logger()->warning('DPD rate limit exceeded', ['error' => $e->getMessage()]);
} catch (ValidationException $e) {
    // Invalid shipment data
    return back()->withErrors(['shipment' => $e->getMessage()]);
} catch (SoapException $e) {
    // SOAP/network error
    logger()->error('DPD SOAP error', ['error' => $e->getMessage()]);
}

try {
    $shipment = Dpd::shipment()
        ->sendingDepot('0000')
        ->sender(/* ... */)
        ->recipient(/* ... */)
        ->create();
} catch (\RuntimeException $e) {
    // Example error message:
    // "DPD API Error: [ERR123] Invalid sender address; [ERR456] Missing 
bash
php artisan vendor:publish --tag="dpd-sdk-config"
bash
composer analyse