PHP code example of very-code-com / cargoboard-php

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

    

very-code-com / cargoboard-php example snippets


use VeryCodeCom\Cargoboard\CargoboardClient;
use VeryCodeCom\Cargoboard\Dto\{Address, Consignee, Line, ShipmentRequest, Shipper};
use VeryCodeCom\Cargoboard\Enum\{CountryCode, PackageType, Product};

$client = CargoboardClient::sandbox('your-api-key');

$request = new ShipmentRequest(
    product: Product::Standard,
    shipper: new Shipper(
        address: new Address('40239', CountryCode::DE, 'Düsseldorf', 'Examplestreet 12a'),
        name: 'Producer ABC GmbH & Co. KG',
        pickupOn: '2026-08-20',
    ),
    consignee: new Consignee(
        address: new Address('41061', CountryCode::DE, 'Mönchengladbach', 'Examplestreet 5'),
        name: 'Consignee ABC AG',
    ),
    // Dimensions in centimetres, weight in kilograms, both PER UNIT.
    lines: [new Line('Werkzeugmaschine', 1, PackageType::EuroPallet, 120, 80, 120, 200.0)],
);

// 1. Get a binding price.
$quotation = $client->quote($request);
echo $quotation->price;            // 90.85 EUR
echo $quotation->runtime;          // 1-2 days

// 2. Book that exact price.
$order = $client->bookQuotation($quotation->id, $request);
echo $order->reference;            // 10374504  <- the shipment number

// 3. Get the labels.
file_put_contents('labels.pdf', $client->fetchLabels($order->id));

// Named constructors
$client = CargoboardClient::sandbox($apiKey);
$client = CargoboardClient::production($apiKey);

// From environment variables (recommended)
$client = new CargoboardClient(CargoboardConfig::fromEnv());

// From array (framework config)
$config = CargoboardConfig::fromArray([
    'api_key' => '...', 'env' => 'sandbox', 'timeout' => 30,
]);

$client = new CargoboardClient(
    CargoboardConfig::fromArray(['api_key' => $key, 'env' => 'sandbox', 'debug' => true]),
    logger: $myPsrLogger,
);

try {
    $client->placeOrder($request);
} catch (\VeryCodeCom\Cargoboard\Exception\CargoboardException $e) {
    echo $e->getRawResponse();   // exact JSON Cargoboard returned (or null)
    echo $e->getDebugReport();   // class + message + raw response + stack trace
}

$parcels = $client->withParcelMode();
$parcels->placeOrder($request);

foreach ($client->fetchTracking($reference)->timeline() as $event) {
    printf("%-5s %s\n", $event->code, $event->describe());
    // 540   Estimates updated: collection 18.08.2026 07:00-15:00, delivery 19.08.2026 06:00 - 21.08.2026 14:00
}

$errors = $client->validateLocally($request);   // list<string>, empty means valid

new CargoboardClient(
    config:    CargoboardConfig,
    transport: TransportInterface       = new CurlTransport(),
    logger:    ?Psr\Log\LoggerInterface = null,
)