PHP code example of tomas-kulhanek / czech-data-box

1. Go to this page and download the library: Download tomas-kulhanek/czech-data-box 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/ */

    

tomas-kulhanek / czech-data-box example snippets


use Symfony\Component\Validator\Validation;

$validator = Validation::createValidatorBuilder()->enableAttributeMapping()->getValidator();
$violations = $validator->validate($request);
if (count($violations) > 0) {
    // ošetřete chyby dříve, než požadavek předáte konektoru
}



use TomasKulhanek\CzechDataBox\Account;
use TomasKulhanek\CzechDataBox\Connector;
use TomasKulhanek\CzechDataBox\Enum\LoginTypeEnum;
use TomasKulhanek\CzechDataBox\Provider\GuzzleClientProvider;
use TomasKulhanek\CzechDataBox\Serializer\SerializerFactory;

$account = new Account();
$account->setLoginName('mujLogin')
    ->setPassword('mojeTajneHeslo')
    ->setLoginType(LoginTypeEnum::NAME_PASSWORD);

$connector = new Connector(SerializerFactory::create(), GuzzleClientProvider::create());

// nejjednodušší ověření přístupových údajů — informace o vlastníkovi schránky
$response = $connector->getOwnerInfoFromLogin2($account);
if (!$response->getStatus()->isOk()) {
    throw new RuntimeException($response->getStatus()->getMessage());
}

echo $response->getOwnerInfo()->getDataBoxId();

use TomasKulhanek\CzechDataBox\Provider\EndpointProvider;
use TomasKulhanek\CzechDataBox\Provider\GuzzleClientProvider;

$provider = GuzzleClientProvider::create();                            // produkce (datovka.gov.cz)
$provider = GuzzleClientProvider::create(EndpointProvider::test());    // test (datovka-test.gov.cz)
$provider = GuzzleClientProvider::create(new EndpointProvider('datovka.cms2.cz')); // vlastní doména (KIVS)



use TomasKulhanek\CzechDataBox\Connector;
use TomasKulhanek\CzechDataBox\DTO\Envelope;
use TomasKulhanek\CzechDataBox\DTO\File;
use TomasKulhanek\CzechDataBox\DTO\Recipient;
use TomasKulhanek\CzechDataBox\DTO\Request\CreateMessage;
use TomasKulhanek\CzechDataBox\Provider\GuzzleClientProvider;
use TomasKulhanek\CzechDataBox\Serializer\SerializerFactory;
use TomasKulhanek\CzechDataBox\Serializer\SplFileInfo;

$connector = new Connector(SerializerFactory::create(), GuzzleClientProvider::create());
// $account vytvoříte podle sekce Rychlý start

$recipient = new Recipient();
$recipient->setDataBoxId('abcdefg')
    ->setOrgUnit('Odbor právní')
    ->setToHand('Jan Novák');

$envelope = new Envelope();
$envelope->setAnnotation('Žádost o vyjádření')
    ->setSenderRefNumber('MUJ-2026/123');

$file = new File();
$file->setMimeType('application/pdf')
    ->setMetaType('main')
    ->setDescription('zadost.pdf')
    ->setEncodedContent(new SplFileInfo('/cesta/zadost.pdf'));

$request = new CreateMessage();
$request->setEnvelope($envelope)
    ->addRecipient($recipient)
    ->addFile($file);

$response = $connector->createMessage($account, $request);
if (!$response->isOk()) {
    throw new RuntimeException($response->getStatus()->getMessage());
}

foreach ($response->getMultipleStatus() as $messageStatus) {
    echo $messageStatus->getDataMessageId() . ': ' . $messageStatus->getStatus()->getMessage() . PHP_EOL;
}



use DateTimeImmutable;
use TomasKulhanek\CzechDataBox\Connector;
use TomasKulhanek\CzechDataBox\DTO\Request\GetListOfReceivedMessages;
use TomasKulhanek\CzechDataBox\Enum\FilterEnum;
use TomasKulhanek\CzechDataBox\Provider\GuzzleClientProvider;
use TomasKulhanek\CzechDataBox\Serializer\SerializerFactory;

$connector = new Connector(SerializerFactory::create(), GuzzleClientProvider::create());

$request = new GetListOfReceivedMessages();
$request->setListFrom(new DateTimeImmutable('-7 days'))
    ->setListTo(new DateTimeImmutable())
    ->setStatusFilter(FilterEnum::DELIVERED, FilterEnum::READ)
    ->setLimit(50);

$response = $connector->getListOfReceivedMessages($account, $request);
if (!$response->getStatus()->isOk()) {
    throw new RuntimeException($response->getStatus()->getMessage());
}

foreach ($response->getRecord() as $record) {
    echo $record->getDataMessageId() . ': ' . $record->getAnnotation() . PHP_EOL;
}



use TomasKulhanek\CzechDataBox\DTO\Request\MarkMessageAsDownloaded;
use TomasKulhanek\CzechDataBox\DTO\Request\MessageDownload;

$markRequest = new MarkMessageAsDownloaded();
$markRequest->setDataMessageId('123456789');
$connector->markMessageAsDownloaded($account, $markRequest);

$downloadRequest = new MessageDownload();
$downloadRequest->setDataMessageId('123456789');

$downloaded = $connector->messageDownload($account, $downloadRequest);
$envelope = $downloaded->getReturnedMessage()->getDataMessage();

foreach ($envelope->getFiles() as $file) {
    echo $file->getDescription() . ' (' . $file->getMetaType() . ')' . PHP_EOL;

    $content = $file->getEncodedContent()?->getContents();
    if ($content !== null) {
        file_put_contents('/cesta/' . $file->getDescription(), $content);
    }
}

$serializer = \TomasKulhanek\CzechDataBox\Serializer\SerializerFactory::create();
$provider = \TomasKulhanek\CzechDataBox\Provider\GuzzleClientProvider::create();
$connector = new \TomasKulhanek\CzechDataBox\Connector($serializer, $provider);

$serializer = \TomasKulhanek\CzechDataBox\Serializer\SerializerFactory::create();
$provider = \TomasKulhanek\CzechDataBox\Provider\SymfonyClientProvider::create();
$connector = new \TomasKulhanek\CzechDataBox\Connector($serializer, $provider);



use TomasKulhanek\CzechDataBox\Account;
use TomasKulhanek\CzechDataBox\Connector;
use TomasKulhanek\CzechDataBox\DTO\BigAttachment;
use TomasKulhanek\CzechDataBox\DTO\BigMessageEnvelope;
use TomasKulhanek\CzechDataBox\DTO\BigMessageFiles;
use TomasKulhanek\CzechDataBox\DTO\ExtFile;
use TomasKulhanek\CzechDataBox\DTO\Request\CreateBigMessage;
use TomasKulhanek\CzechDataBox\DTO\Request\UploadAttachment;
use TomasKulhanek\CzechDataBox\Enum\LoginTypeEnum;
use TomasKulhanek\CzechDataBox\Provider\GuzzleClientProvider;
use TomasKulhanek\CzechDataBox\Serializer\SerializerFactory;
use TomasKulhanek\CzechDataBox\Serializer\SplFileInfo;

$account = new Account();
$account->setPassword('mojeTajneHeslo')
    ->setLoginName('mujLogin')
    ->setLoginType(LoginTypeEnum::NAME_PASSWORD);

$connector = new Connector(SerializerFactory::create(), GuzzleClientProvider::create());

// 1) Nahrání přílohy (volá se zvlášť pro každou přílohu, součet max. 100 MiB)
$attachment = new BigAttachment();
$attachment->setMimeType('application/pdf')
    ->setDescription('smlouva.pdf')
    ->setEncodedContent(new SplFileInfo('/cesta/ke/smlouva.pdf'));

$uploadRequest = new UploadAttachment();
$uploadRequest->setFile($attachment);

$uploadResponse = $connector->uploadAttachment($account, $uploadRequest);
if (!$uploadResponse->getStatus()->isOk()) {
    throw new RuntimeException($uploadResponse->getStatus()->getMessage());
}
$attachmentId = $uploadResponse->getAttachmentId(); // např. "ATT123456"

// 2) Odeslání zprávy odkazující na nahranou přílohu
$envelope = new BigMessageEnvelope();
$envelope->setType('V')
    ->setRecipientId('abcdefg')          // dbIDRecipient
    ->setAnnotation('Smlouva o dílo');   // dmAnnotation

$extFile = new ExtFile();
$extFile->setMetaType('main')            // hlavní příloha zprávy
    ->setAttachmentId($attachmentId)
    ->setAttachmentHash1($uploadResponse->getAttachmentHash1()->getValue())
    ->setAttachmentHash1Algorithm($uploadResponse->getAttachmentHash1()->getAlgorithm())
    ->setAttachmentHash2($uploadResponse->getAttachmentHash2()->getValue())
    ->setAttachmentHash2Algorithm($uploadResponse->getAttachmentHash2()->getAlgorithm());

$files = new BigMessageFiles();
$files->addExtFile($extFile);

$request = new CreateBigMessage();
$request->setEnvelope($envelope);
$request->setFiles($files);

$response = $connector->createBigMessage($account, $request);
if ($response->getStatus()->isOk()) {
    // zpráva byla úspěšně podána
}



use TomasKulhanek\CzechDataBox\DTO\Request\NewAccessData2;
use TomasKulhanek\CzechDataBox\DTO\Request\SetOpenAddressing;

$response = $connector->setOpenAddressing($account, new SetOpenAddressing()->setDataBoxId('abcdefg'));
if (!$response->getStatus()->isOk()) {
    throw new RuntimeException($response->getStatus()->getMessage());
}

$request = new NewAccessData2();
$request->setDataBoxId('abcdefg')
    ->setIsdsId('a23456789012')
    ->setFeePaid(true);

$newAccessData = $connector->newAccessData2($account, $request);
echo $newAccessData->getAccessDataId();