PHP code example of dpdconnect / php-sdk

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

    

dpdconnect / php-sdk example snippets


use DpdConnect\Sdk\ClientBuilder;

$clientBuilder = new ClientBuilder();

// Authenticate by username + password (JWT is fetched automatically)
$client = $clientBuilder->buildAuthenticatedByPassword('your-username', 'your-password');

// Or authenticate with an existing JWT token
$client = $clientBuilder->buildAuthenticatedByJwtToken('your-jwt-token');

use DpdConnect\Sdk\Objects\ObjectFactory;
use DpdConnect\Sdk\Objects\ShipmentOrder;
use DpdConnect\Sdk\Objects\ShipmentOrder\Contact\Sender;
use DpdConnect\Sdk\Objects\ShipmentOrder\Contact\Receiver;
use DpdConnect\Sdk\Objects\ShipmentOrder\PrintOptions;
use DpdConnect\Sdk\Objects\ShipmentOrder\Shipment;
use DpdConnect\Sdk\Objects\ShipmentOrder\Shipment\Parcel;

$printOptions = ObjectFactory::create(PrintOptions::class, [
    'printerLanguage' => 'PDF',
    'paperFormat'     => 'A4',
]);

$sender = ObjectFactory::create(Sender::class, [
    'companyname'       => 'My Company B.V.',
    'name1'             => 'John Do',
    'street'            => 'John Do Street',
    'housenumber'       => '9',
    'postalcode'        => '1101BM',
    'city'              => 'Amsterdam',
    'country'           => 'NL',
    'email'             => '[email protected]',
    'phoneNumber'       => '0612345678',
    'commercialAddress' => true,
]);

$receiver = ObjectFactory::create(Receiver::class, [
    'name1'             => 'Jane Do',
    'street'            => 'Jane Do Street',
    'housenumber'       => '10',
    'postalcode'        => '1101BM',
    'city'              => 'Amsterdam',
    'country'           => 'NL',
    'phoneNumber'       => '0698765432',
    'commercialAddress' => false,
]);

$parcel = ObjectFactory::create(Parcel::class, [
    'customerReferences' => ['order-12345'],
    'weight'             => 500,
]);

$shipment = ObjectFactory::create(Shipment::class, [
    'orderId'      => 'order-12345',
    'sendingDepot' => '0522',
    'sender'       => $sender,
    'receiver'     => $receiver,
    'product'      => ['productCode' => 'CL'],
    'parcels'      => [$parcel],
]);

$shipmentOrder = ObjectFactory::create(ShipmentOrder::class, [
    'printOptions' => $printOptions,
    'createLabel'  => true,
    'shipments'    => [$shipment],
]);

$response = $client->getShipment()->createShipmentOrder($shipmentOrder);

// List parcelshops near a location
$parcelshops = $client->getParcelshop()->getList([
    'longitude'  => 4.9041,
    'latitude'   => 52.3676,
    'countryIso' => 'NL',
    'limit'      => 10,
]);

// Get a specific parcelshop by ID
$parcelshop = $client->getParcelshop()->get('NL10566');

$status = $client->getParcel()->getStatus('parcel-id');
$label  = $client->getParcel()->getLabel('parcel-id');

// For staging or self-hosted environments
$clientBuilder = new ClientBuilder('https://api.staging.dpdconnect.nl');
$client = $clientBuilder->buildAuthenticatedByPassword('username', 'password');
bash
composer 
bash
php bin/phpcs          # PHP_CodeSniffer
php bin/phplint        # PHP lint check
phpmd . text phpmd.xml --exclude vendor