PHP code example of sonnenglas / dhl-parcel-de-sdk

1. Go to this page and download the library: Download sonnenglas/dhl-parcel-de-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/ */

    

sonnenglas / dhl-parcel-de-sdk example snippets


use Sonnenglas\DhlParcelDe\Dhl;
use Sonnenglas\DhlParcelDe\Enums\ShipmentProduct;
use Sonnenglas\DhlParcelDe\ValueObjects\Address;
use Sonnenglas\DhlParcelDe\ValueObjects\Shipment;
use Sonnenglas\DhlParcelDe\ValueObjects\Package;

// Initialize with your DHL API credentials
$dhl = new Dhl(
    username: 'your_username',
    password: 'your_password',
    apiKey: 'your_api_key',
    productionMode: false // Set to true for production
);

$shipmentService = $dhl->getShipmentService();

// 1. Create the shipper address
$shipper = new Address(
    name: 'Your Company Name',
    addressStreet: 'Your Street 123',
    postalCode: '12345',
    city: 'Your City',
    country: 'DE',
    email: '[email protected]'
);

// 2. Create the recipient address
$recipient = new Address(
    name: 'John Doe',
    addressStreet: 'Customer Street 456',
    postalCode: '54321',
    city: 'Customer City',
    country: 'DE',
    email: '[email protected]',
    phone: '123456789'
);

// 3. Define the package details
$package = new Package(
    height: 200, // in mm
    length: 300, // in mm
    width: 400,  // in mm
    weight: 2000 // in g
);

// 4. Create the shipment object
$shipment = new Shipment(
    product: ShipmentProduct::DhlPacket,
    billingNumber: '33333333330102', // Your DHL billing number
    referenceNo: '123456789',        // Your reference number (min 8 characters)
    shipper: $shipper,
    recipient: $recipient,
    package: $package
);

// 5. Send the shipment to DHL
try {
    $response = $shipmentService
        ->setShipments([$shipment])
        ->createShipment();
    
    // The shipment was created successfully
    $shipmentNumber = $response->shipmentNumber;
    $labelUrl = $response->labelUrl;
    
    echo "Shipment created with number: " . $shipmentNumber;
    echo "Label URL: " . $labelUrl;
} catch (Exception $e) {
    // Handle any errors
    echo "Error: " . $e->getMessage();
    echo "API Error Response: " . $shipmentService->getLastErrorResponse();
}

$packstationAddress = new Address(
    name: 'Max Mustermann',                    // Customer name
    addressStreet: '',                         // Ignored for packstation
    postalCode: '50667',                       // Packstation postal code
    city: 'Köln',                            // Packstation city
    country: 'DE',                            // Must be 'DE' for packstations
    state: '',
    email: '[email protected]',      // Optional
    phone: '',                                // Optional
    additionalInfo: '',                       // Ignored for packstation
    company: '',                              // Must be empty for packstation
    packstationId: 171,                       // 3-digit packstation number
    packstationCustomerNumber: '1234567890'   // Customer's DHL account number
);

use Sonnenglas\DhlParcelDe\Dhl;
use Sonnenglas\DhlParcelDe\Enums\ShipmentProduct;
use Sonnenglas\DhlParcelDe\ValueObjects\Address;
use Sonnenglas\DhlParcelDe\ValueObjects\Shipment;
use Sonnenglas\DhlParcelDe\ValueObjects\Package;

// Initialize DHL client
$dhl = new Dhl($username, $password, $apiKey, $productionMode);
$shipmentService = $dhl->getShipmentService();

// Sender address
$shipper = new Address(
    name: 'Your Company',
    addressStreet: 'Your Street 123',
    postalCode: '12345',
    city: 'Your City',
    country: 'DE'
);

// Packstation recipient
$recipient = new Address(
    name: 'Max Mustermann',
    addressStreet: '',                         // Will be ignored
    postalCode: '50667',                       // Packstation location
    city: 'Köln',                            // Packstation city
    country: 'DE',                           // Must be DE
    packstationId: 171,                       // 3-digit packstation number
    packstationCustomerNumber: '1234567890'   // Customer's DHL account number
);

// Package details
$package = new Package(
    height: 200,    // mm
    length: 300,    // mm
    width: 150,     // mm
    weight: 1000    // grams
);

// Create shipment
$shipment = new Shipment(
    product: ShipmentProduct::DhlPacket,
    billingNumber: '33333333330102',
    referenceNo: 'REF123456789',
    shipper: $shipper,
    recipient: $recipient,
    package: $package
);

// Send shipment
try {
    $response = $shipmentService->setShipments([$shipment])->createShipment();
    echo "Packstation shipment created successfully!";
} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
}

try {
    $success = $shipmentService->deleteShipment('1234567890');
    
    if ($success) {
        echo "Shipment deleted successfully";
    } else {
        echo "Failed to delete shipment";
        echo "Error: " . $shipmentService->getLastErrorResponse();
    }
} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
}

$shipmentService->setProfile('CUSTOM_PROFILE');

use Sonnenglas\DhlParcelDe\Enums\LabelFormat;

$shipmentService->setLabelFormat(LabelFormat::A4);



declare(strict_types=1);


use Sonnenglas\DhlParcelDe\Enums\ShipmentProduct;
use Sonnenglas\DhlParcelDe\ValueObjects\Address;
use Sonnenglas\DhlParcelDe\ValueObjects\Shipment;
use Sonnenglas\DhlParcelDe\ValueObjects\Package;

$user = 'your_api_username';
$pass = 'your_api_password';
$key = 'your_api_key_here';

$productionMode = false;

$dhl = new Dhl($user, $pass, $key, $productionMode);

$shipmentService = $dhl->getShipmentService();

$shipper = new Address(
    name: 'Stellar Widgets GmbH',
    addressStreet: 'Industrieweg 42',
    postalCode: '28195',
    city: 'Bremen',
    country: 'DE',
    state: '',
    email: '[email protected]',
    phone: '',
    additionalInfo: '',
);

$recipient = new Address(
    name: 'Anna Schmidt',
    addressStreet: 'Rosenstraße 67',
    postalCode: '10115',
    city: 'Berlin',
    country: 'DE',
    state: '',
    email: '[email protected]',
    phone: '01761234567',
    additionalInfo: 'Second Floor',
);

$package = new Package(
    height: 200,
    length: 200,
    width: 400,
    weight: 4000,
);

$shipment = new Shipment(
    product: ShipmentProduct::DhlPacket,
    billingNumber: '33333333330102',
    referenceNo: 'ORD987654321',
    shipper: $shipper,
    recipient: $recipient,
    package: $package,
);

try {
    $response = $shipmentService->setShipments([$shipment])
        ->createShipment();
    
    echo "Result:";
    var_dump($response);
} catch (Exception $e) {
    echo $shipmentService->getLastErrorResponse();
}

$customer = new Address(
    name: 'Max Mustermann',
    addressStreet: 'Hauptstraße',  // street name only
    addressHouse: '1',             // house number — separate field
    postalCode: '10115',
    city: 'Berlin',
    country: 'DE'
);