PHP code example of blobswop / dpd-php-api

1. Go to this page and download the library: Download blobswop/dpd-php-api 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/ */

    

blobswop / dpd-php-api example snippets


// set development environment
\Dpd\Api::dev();
$response = null;

try {
    $response = \Dpd\Api::me()->auth(
        new \Dpd\Business\Credentials('sandboxdpd', 'password', 'en_US')
    );
    
    if ($response instanceof \Dpd\Business\AuthenticationFault) {
        // authentication fault
        print_r($response);     
    } elseif ($response instanceof \Dpd\Business\Login) {
        // authentication success - save token
        print_r($response);
    }
} catch(\Throwable $e) {
    print_r($e);
}

// set development environment
\Dpd\Api::dev();
$response = null;

try {
    $authentication =
        (new \Dpd\Business\Authentication)
            ->setDelisId('sandboxdpd')
            ->setAuthToken('L***R')
            ->setMessageLanguage('en_US');

    $storeOrders =
        (new \Dpd\Business\StoreOrders())
            ->setPrintOptions(
                (new \Dpd\Business\PrintOptions())
                    ->addPrintOption(
                        (new \Dpd\Business\PrintOption())
                            ->setPaperFormatA4()
                            ->setOutputFormat(\Dpd\Business\OutputFormatType::multipageImage())
                    )
                    ->setSplitByParcel(true)
            )
            ->addOrder(
                (new \Dpd\Business\ShipmentServiceData())
                    ->setGeneralShipmentData(
                        (new \Dpd\Business\GeneralShipmentData())
                            ->setProductDpdClassic()
                            ->setSendingDepot('0141')
                            ->setMpsWeight(500)
                            ->setMpsVolume(100 * 10 * 50)
                            ->setSender(
                                (new \Dpd\Business\AddressWithType())
                                    ->setAddressTypeCommercial()
                                    ->setName1('Sender Company GmbH')
                                    ->setName2('Company Sender Person')
                                    ->setStreet('Residenzstraße')
                                    ->setHouseNo(1)
                                    ->setCountry('DE')
                                    ->setZipCode('80333')
                                    ->setCity('München')
                            )
                            ->setRecipient(
                                (new \Dpd\Business\AddressWithType())
                                    ->setAddressTypePrivate()
                                    ->setName1('Person Name')
                                    ->setStreet('Neue Mainzer Str.')
                                    ->setHouseNo('52-58')
                                    ->setCountry('DE')
                                    ->setZipCode('60311')
                                    ->setCity('Frankfurt am Main')
                            )
                            ->setIdentificationNumber('Your-Shipment-Id')
                            ->setMpsCustomerReferenceNumber1('Customer reference 1')
                    )
                    ->addParcel(
                        (new \Dpd\Business\Parcel())
                            ->setWeight(500)
                            ->setAddServiceParcelBox()
                    )
                    ->setProductAndServiceData(
                        (new \Dpd\Business\ProductAndServiceData())
                            ->setOrderTypeConsignment()
                            ->setFood(false)
                    )
            )
            ->addOrder(
                (new \Dpd\Business\ShipmentServiceData())
                    ->setGeneralShipmentData(
                        (new \Dpd\Business\GeneralShipmentData())
                            ->setProductDpdClassic()
                            ->setSendingDepot('0141')
                            ->setMpsWeight(500)
                            ->setMpsVolume(100 * 10 * 50)
                            ->setSender(
                                (new \Dpd\Business\AddressWithType())
                                    ->setAddressTypeCommercial()
                                    ->setName1('Sender Company GmbH')
                                    ->setName2('Company Sender Person')
                                    ->setStreet('Residenzstraße')
                                    ->setHouseNo(1)
                                    ->setCountry('DE')
                                    ->setZipCode('80333')
                                    ->setCity('München')
                            )
                            ->setRecipient(
                                (new \Dpd\Business\AddressWithType())
                                    ->setAddressTypePrivate()
                                    ->setName1('Person Name')
                                    ->setStreet('Neue Mainzer Str.')
                                    ->setHouseNo('52-58')
                                    ->setCountry('DE')
                                    ->setZipCode('60311')
                                    ->setCity('Frankfurt am Main')
                            )
                            ->setIdentificationNumber('Your-Return-Shipment-Id')
                            ->setMpsCustomerReferenceNumber1('Customer reference 1')
                    )
                    ->addParcel(
                        (new \Dpd\Business\Parcel())
                            ->setWeight(500)
                            ->setAddServiceParcelBox()
                            ->setReturns(true)
                    )
                    ->setProductAndServiceData(
                        (new \Dpd\Business\ProductAndServiceData())
                            ->setOrderTypeConsignment()
                            ->setFood(false)
                    )
            );

    $response = \Dpd\Api::me()->storeOrders($authentication, $storeOrders);
    
    if ($response instanceof \Dpd\Business\AuthenticationFault) {
        // authentication fault
        print_r($response);     
    } elseif ($response instanceof \Dpd\Business\StoreOrdersResponseType) {
    
        print_r($response);
        
        foreach ($response->getShipmentResponses() as $shipmentResponse) {
            if (
                $shipmentResponse instanceof \Dpd\Business\ShipmentResponse
                && $shipmentResponse->getParcelInformation() instanceof \Dpd\Business\ParcelInformationType
                && $shipmentResponse->getParcelInformation()->getParcelLabelNumber()
                && $shipmentResponse->getParcelInformation()->getOutput() instanceof \Dpd\Business\OutputType
                && $shipmentResponse->getParcelInformation()->getOutput()->getContent()
            ) {
                if ($shipmentResponse->getParcelInformation()->getOutput()->getFormat() == \Dpd\Business\OutputFormatType::TYPE_PDF) {
                    file_put_contents($shipmentResponse->getParcelInformation()->getParcelLabelNumber() . '.pdf', $shipmentResponse->getParcelInformation()->getOutput()->getContent());
                } elseif ($shipmentResponse->getParcelInformation()->getOutput()->getFormat() == \Dpd\Business\OutputFormatType::TYPE_MULTIPAGE_IMAGE) {
                    file_put_contents($shipmentResponse->getParcelInformation()->getParcelLabelNumber() . '.gif', $shipmentResponse->getParcelInformation()->getOutput()->getContent());
                }
            }
        }
    }
} catch(\Throwable $e) {
    // process exception
}

\Dpd\Api::dev();
$response = null;

try {
    $authentication =
        (new \Dpd\Business\Authentication)
            ->setDelisId('sandboxdpd')
            ->setAuthToken('L***R')
            ->setMessageLanguage('en_US');

    $response = 
        \Dpd\Api::me()->getTrackingData(
            $authentication,
            new \Dpd\Business\GetTrackingData('09981122330100')
        );
    
    if ($response instanceof \Dpd\Business\AuthenticationFault) {
        // authentication fault
        print_r($response);  
    } elseif ($response instanceof \Dpd\Business\TrackingResult) {
        print_r($response);
    }
} catch(\Throwable $e) {
    print_r($e);
}
bash
$ composer