PHP code example of postbode / postbode-api

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

    

postbode / postbode-api example snippets


use Postbode\PostbodeApiClient;

$postbode = new PostbodeApiClient('your-api-key');

foreach ($postbode->mailboxes->list() as $mailbox) {
    printf("%s (%s): € %.2f available\n", $mailbox->name, $mailbox->customerCode, $mailbox->balance->available);
}

$postbode = new PostbodeApiClient('your-api-key', new GuzzleHttp\Client(['timeout' => 30]));

use Postbode\Enum\PostalPlex;
use Postbode\Enum\PostalPrinting;
use Postbode\Enum\ShippingType;
use Postbode\Request\PostalRequest;

$request = PostalRequest::make('PSBD', $envelopeUuid)
    ->addDocumentFromFile(__DIR__ . '/invoice.pdf')
    ->shipping(ShippingType::NL_FAST)
    ->printing(PostalPrinting::COLOR)
    ->plex(PostalPlex::DUPLEX)
    ->customerReference('INV-1234')
    ->metadata(['invoice_id' => 1234])
    ->send();

$postal = $postbode->postals->create($request);

echo $postal->reference;                 // PSBD-000123
echo $postal->status->name;              // Sent
echo $postal->financial->price->amountInclVat;

$template = PostalRequest::make('PSBD', $envelopeUuid)->shipping(ShippingType::NL_SLOW);

foreach ($invoices as $invoice) {
    $postbode->postals->create(
        $template->addDocumentFromFile($invoice->path)->customerReference($invoice->number)->send(),
    );
}

$postal = $postbode->postals->create($request->send(false));

file_put_contents('preview.pdf', $postbode->postals->document($postal->uuid));

$postbode->postals->send($postal->uuid);   // or ->cancel($postal->uuid)

$calculation = $postbode->postals->calculate(
    envelope: $envelopeUuid,
    pages: 3,
    mailbox: 'PSBD',
    shipping: ShippingType::NL_FAST,
);

echo $calculation->totalInVat;

foreach ($calculation->elements as $element) {
    printf("%-30s € %.2f\n", $element->description, $element->price);
}

use Postbode\Enum\PostalStatus;

$postal = $postbode->postals->get($uuid);

if ($postal->status->isDelivered()) {
    echo 'Delivered';
}

if ($postal->tracking->isAvailable()) {
    echo $postal->tracking->url;
}

foreach ($postbode->postals->list('PSBD', limit: 25, status: PostalStatus::IN_TRANSIT) as $item) {
    echo $item->reference, ': ', $item->status->name, PHP_EOL;
}

$tracked = $postbode->tracking->track($reference, 'NL', '1234AB');
echo $tracked->status;

$postbode->postals->create([
    'mailbox' => 'PSBD',
    'envelope' => $envelopeUuid,
    'documents' => [['filename' => 'invoice.pdf', 'content' => base64_encode($pdf)]],
]);

use Postbode\Enum\PostalStatus;
use Postbode\Enum\ShippingType;

PostalStatus::DELIVERED->value;           // 150
PostalStatus::DELIVERED->label();         // 'Delivered'
PostalStatus::DELIVERED->isFinal();       // true
ShippingType::NL_REGISTERED->isTracked(); // true

$postal->status->code;     // PostalStatus|null — null if the API added a code we do not know
$postal->status->rawCode;  // int — always what the API actually sent
$postal->status->name;     // string — the API's own description

use Postbode\Exception\AuthenticationException;
use Postbode\Exception\NotFoundException;
use Postbode\Exception\PostbodeException;
use Postbode\Exception\TransportException;
use Postbode\Exception\ValidationException;

try {
    $postbode->postals->create($request);
} catch (ValidationException $e) {
    foreach ($e->getErrors() as $field => $messages) {
        echo $field, ': ', implode(' ', $messages), PHP_EOL;
    }
} catch (AuthenticationException $e) {
    // 401 or 403 — bad key, or no access to this mailbox
} catch (NotFoundException $e) {
    // 404 — the envelope, mailbox or item does not exist
} catch (TransportException $e) {
    // the API could not be reached at all
} catch (PostbodeException $e) {
    // anything else, including 400 and 5xx
    echo $e->getMessage();
}
bash
POSTBODE_API_KEY=your-key php examples/list-mailboxes.php