PHPackages                             sonnenglas/dhl-parcel-de-sdk - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. sonnenglas/dhl-parcel-de-sdk

ActiveLibrary[HTTP &amp; Networking](/categories/http)

sonnenglas/dhl-parcel-de-sdk
============================

Unofficial PHP SDK for DHL Parcel DE (Post &amp; Parcel Germany) Rest API

v2.7.0(3w ago)21MITPHPPHP &gt;=8.1

Since Aug 28Pushed 3w ago2 watchersCompare

[ Source](https://github.com/sonnenglas/dhl-parcel-de-sdk)[ Packagist](https://packagist.org/packages/sonnenglas/dhl-parcel-de-sdk)[ RSS](/packages/sonnenglas-dhl-parcel-de-sdk/feed)WikiDiscussions master Synced 2w ago

READMEChangelogDependencies (18)Versions (30)Used By (0)

DHL Parcel DE SDK
=================

[](#dhl-parcel-de-sdk)

A PHP SDK for interacting with the DHL Parcel DE Shipping API (Post &amp; Parcel Germany). This package allows you to easily create shipments, print labels, and manage shipments through the DHL API.

[![PHP Version](https://camo.githubusercontent.com/854124dd57cfd3aad3184fca9760bf1f33a5ec1e5d080cfbe8aa4e3337ba46e6/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344382e302d3838393242462e737667)](https://www.php.net/)[![License](https://camo.githubusercontent.com/074b89bca64d3edc93a1db6c7e3b1636b874540ba91d66367c0e5e354c56d0ea/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e737667)](LICENSE)

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require sonnenglas/dhl-parcel-de-sdk
```

Requirements
------------

[](#requirements)

- PHP 8.0 or higher
- Composer
- DHL API credentials (username, password, and API key)

Features
--------

[](#features)

- Create shipments and generate labels
- Delete shipments
- Support for various DHL products (DHL Paket, DHL Paket International, etc.)
- Address validation
- Custom package dimensions and weight
- Support for production and sandbox environments

Usage
-----

[](#usage)

### Initialization

[](#initialization)

```
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();
```

### Creating a Shipment

[](#creating-a-shipment)

```
// 1. Create the shipper address
$shipper = new Address(
    name: 'Your Company Name',
    addressStreet: 'Your Street 123',
    postalCode: '12345',
    city: 'Your City',
    country: 'DE',
    email: 'info@example.com'
);

// 2. Create the recipient address
$recipient = new Address(
    name: 'John Doe',
    addressStreet: 'Customer Street 456',
    postalCode: '54321',
    city: 'Customer City',
    country: 'DE',
    email: 'customer@example.com',
    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();
}
```

### DHL Packstation Support

[](#dhl-packstation-support)

The SDK supports DHL Packstation deliveries, allowing customers to receive packages at automated parcel stations throughout Germany.

#### What is a Packstation?

[](#what-is-a-packstation)

DHL Packstations are automated parcel stations where customers can receive and send packages 24/7. They are located throughout Germany and provide a convenient alternative to home delivery.

#### Requirements for Packstation Delivery

[](#requirements-for-packstation-delivery)

To send a package to a DHL Packstation, you need:

1. **Packstation ID** - A 3-digit number (100-999) identifying the specific packstation
2. **Customer's DHL Account Number (Postnummer)** - A 6-10 digit number that customers receive when they register for DHL services
3. **Packstation Location** - The postal code and city where the packstation is located
4. **Customer Name** - The name of the person who will collect the package

#### Creating a Packstation Address

[](#creating-a-packstation-address)

```
$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: 'max.mustermann@example.com',      // 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
);
```

#### Complete Packstation Shipment Example

[](#complete-packstation-shipment-example)

```
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();
}
```

#### Packstation Validation Rules

[](#packstation-validation-rules)

The SDK validates packstation addresses according to DHL requirements:

- **Packstation ID**: Must be a 3-digit integer between 100 and 999
- **Customer Number**: Must be 6-10 digits (string)
- **Country**: Must be 'DE' (packstations are only available in Germany)
- **Both Fields Required**: If you provide one packstation field, you must provide both
- **Company Field**: Must be empty for packstation deliveries (private customers only)

#### API Format

[](#api-format)

The SDK automatically converts packstation addresses to the correct DHL API format:

**Packstation Address (Locker format):**

```
{
    "name": "Max Mustermann",
    "lockerID": 171,
    "postNumber": "1234567890",
    "city": "Köln",
    "postalCode": "50667",
    "country": "DEU"
}
```

**Regular Address (ContactAddress format):**

```
{
    "name1": "John Doe",
    "addressStreet": "Musterstraße 123",
    "postalCode": "50667",
    "city": "Köln",
    "country": "DEU"
}
```

#### Finding Packstations

[](#finding-packstations)

Customers can find nearby packstations using:

- DHL website:
- DHL mobile app
- DHL customer portal

#### Customer Registration

[](#customer-registration)

Customers need to register for DHL services to get their Postnummer (customer number):

- Online at:
- At DHL retail locations
- Through the DHL mobile app

The Postnummer is required for all packstation deliveries and is unique to each customer.

### Deleting a Shipment

[](#deleting-a-shipment)

```
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();
}
```

### Using a Custom Profile

[](#using-a-custom-profile)

```
$shipmentService->setProfile('CUSTOM_PROFILE');
```

### Setting Label Format

[](#setting-label-format)

```
use Sonnenglas\DhlParcelDe\Enums\LabelFormat;

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

Complete Example
----------------

[](#complete-example)

Here's a complete example showing how to use the SDK to create a shipment:

```
