1. Go to this page and download the library: Download n1ebieski/ksef-php-client 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/ */
n1ebieski / ksef-php-client example snippets
use N1ebieski\KSEFClient\ClientBuilder;
use N1ebieski\KSEFClient\ValueObjects\Mode;
use N1ebieski\KSEFClient\Factories\EncryptionKeyFactory;
$client = new ClientBuilder()
->withMode(Mode::Production) // Choice between: Test, Demo, Production
->withApiUrl($_ENV['KSEF_API_URL']) // Optional, default is set by Mode selection
->withHttpClient(new \GuzzleHttp\Client([])) // Optional, default is set by Psr18ClientDiscovery::find()
->withSessionToken($_ENV['SESSION_TOKEN']) // Optional, if present, auto authorization is skipped
->withApiToken($_ENV['KSEF_KEY']) // Required for API Token authorization
->withKSEFPublicKeyPath($_ENV['PATH_TO_KSEF_PUBLIC_KEY']) // Required (including for API Token authorization and encryption), you can find it on https://ksef.mf.gov.pl
->withCertificatePath($_ENV['PATH_TO_CERTIFICATE'], $_ENV['CERTIFICATE_PASSPHRASE']) // Required .p12 file for Certificate authorization
->withEncryptionKey(EncryptionKeyFactory::makeRandom()) // Optional for online resources,
use N1ebieski\KSEFClient\Requests\Common\Status\StatusRequest;
use N1ebieski\KSEFClient\Requests\ValueObjects\ReferenceNumber;
$commonStatus = $client->common()->status(new StatusRequest(
referenceNumber: ReferenceNumber::from('20250508-EE-B395BBC9CD-A7DB4E6095-BD')
))->object();
use N1ebieski\KSEFClient\ClientBuilder;
$client = new ClientBuilder()
->withApiToken($_ENV['KSEF_KEY'])
->withKSEFPublicKeyPath($_ENV['PATH_TO_KSEF_PUBLIC_KEY'])
->withNIP('NIP_NUMBER')
->build();
// Do something with the available resources
$client->online()->session()->terminate();
use N1ebieski\KSEFClient\ClientBuilder;
$client = new ClientBuilder()
->withCertificatePath($_ENV['PATH_TO_CERTIFICATE'], $_ENV['CERTIFICATE_PASSPHRASE'])
->withKSEFPublicKeyPath($_ENV['PATH_TO_KSEF_PUBLIC_KEY'])
->withNIP('NIP_NUMBER')
->build();
// Do something with the available resources
$client->online()->session()->terminate();
use N1ebieski\KSEFClient\ClientBuilder;
use N1ebieski\KSEFClient\Requests\Online\Session\AuthorisationChallenge\AuthorisationChallengeRequest;
use N1ebieski\KSEFClient\Requests\Online\Session\InitSigned\InitSignedXmlRequest;
use N1ebieski\KSEFClient\Requests\Online\Session\DTOs\InitSessionSigned;
$client = new ClientBuilder()
->withKSEFPublicKeyPath($_ENV['PATH_TO_KSEF_PUBLIC_KEY'])
->build();
$nip = 'NIP_NUMBER';
$authorisationChallengeResponse = $client->online()->session()->authorisationChallenge([
'contextIdentifier' => [
'subjectIdentifierByGroup' => [
'subjectIdentifierByCompany' => $nip
]
]
])->object();
$xml = InitSessionSigned::from([
'challenge' => $authorisationChallengeResponse->challenge,
'timestamp' => $authorisationChallengeResponse->timestamp,
'identifier' => $nip
])->toXml();
$signedXml = // Sign a xml document via Szafir, ePUAP etc.
$initSignedResponse = $client->online()->session()->initSigned(
new InitSignedXmlRequest($signedXml)
)->object();
$client = $client->withSessionToken($initSignedResponse->sessionToken->token);
// Do something with the available resources
$client->online()->session()->terminate();
use N1ebieski\KSEFClient\Requests\Common\Status\StatusRequest;
$response = $client->common()->status(
new StatusRequest(...)
)->object();
use N1ebieski\KSEFClient\Requests\Online\Session\AuthorisationChallenge\AuthorisationChallengeRequest;
$response = $client->online()->session()->authorisationChallenge(
new AuthorisationChallengeRequest(...)
)->object();
use N1ebieski\KSEFClient\Requests\Online\Session\InitToken\InitTokenRequest;
$response = $client->online()->session()->initToken(
new InitTokenRequest(...)
)->object();
use N1ebieski\KSEFClient\Requests\Online\Session\InitSigned\InitSignedRequest;
$response = $client->online()->session()->initSigned(
new InitSignedRequest(...)
)->object();
use N1ebieski\KSEFClient\Requests\Online\Session\InitSigned\InitSignedXmlRequest;
$response = $client->online()->session()->initSigned(
new InitSignedXmlRequest($signedXml)
)->object();
use N1ebieski\KSEFClient\Requests\Online\Session\Status\StatusRequest;
$response = $client->online()->session()->status(
new StatusRequest(...)
)->object();
use N1ebieski\KSEFClient\Requests\Online\Invoice\Get\GetRequest;
$response = $client->online()->invoice()->get(
new GetRequest(...)
)->body();
use N1ebieski\KSEFClient\Requests\Online\Invoice\Send\SendRequest;
$response = $client->online()->invoice()->send(
new SendRequest(...)
)->object();
use N1ebieski\KSEFClient\Requests\Online\Invoice\Status\StatusRequest;
$response = $client->online()->invoice()->status(
new StatusRequest(...)
)->object();
use N1ebieski\KSEFClient\Requests\Online\Query\Invoice\Sync\SyncRequest;
$response = $client->online()->query()->invoice()->sync(
new SyncRequest(...)
)->object();
use N1ebieski\KSEFClient\Requests\Online\Query\Invoice\Async\Init\InitRequest;
$response = $client->online()->query()->invoice()->async()->init(
new InitRequest(...)
)->object();
use N1ebieski\KSEFClient\Requests\Online\Query\Invoice\Async\Status\StatusRequest;
$response = $client->online()->query()->invoice()->async()->status(
new StatusRequest(...)
)->object();
use N1ebieski\KSEFClient\Requests\Online\Query\Invoice\Async\Fetch\FetchRequest;
$response = $client->online()->query()->invoice()->async()->fetch(
new FetchRequest(...)
)->body();
use N1ebieski\KSEFClient\ClientBuilder;
use N1ebieski\KSEFClient\Support\Utility;
use N1ebieski\KSEFClient\Testing\Fixtures\Requests\Online\Invoice\Send\SendFakturaSprzedazyTowaruRequestFixture;
use N1ebieski\KSEFClient\ValueObjects\Mode;
$client = new ClientBuilder()
->withMode(Mode::Test)
->withApiToken($_ENV['KSEF_KEY'])
->withLogXmlPath(__DIR__ . '/../var/xml/')
->withKSEFPublicKeyPath(__DIR__ . '/../config/keys/publicKey.pem')
->build();
Utility::retry(function () use ($client) {
$statusResponse = $client->online()->session()->status()->object();
if ($statusResponse->processingCode === 315) {
return $statusResponse;
}
});
// Send an invoice
$sendResponse = $client->online()->invoice()->send(
new SendFakturaSprzedazyTowaruRequestFixture()->withTodayDate()->data
)->object();
// Check status of invoice generation
Utility::retry(function () use ($client, $sendResponse) {
$statusResponse = $client->online()->invoice()->status([
'invoiceElementReferenceNumber' => $sendResponse->elementReferenceNumber
])->object();
if ($statusResponse->processingCode === 200) {
return $statusResponse;
}
});
// Close session (only then will the UPO be generated)
$client->online()->session()->terminate();
// We don't need to authorize for UPO
$client = new ClientBuilder()
->withMode(Mode::Test)
->withKSEFPublicKeyPath(__DIR__ . '/../config/keys/publicKey.pem')
->build();
// Check status of UPO generation
$commonStatus = Utility::retry(function () use ($client, $sendResponse) {
$commonStatus = $client->common()->status([
'referenceNumber' => $sendResponse->referenceNumber
])->object();
if ($commonStatus->processingCode === 200) {
return $commonStatus;
}
});
$xml = base64_decode($commonStatus->upo);
use N1ebieski\KSEFClient\ClientBuilder;
use N1ebieski\KSEFClient\Factories\EncryptionKeyFactory;
use N1ebieski\KSEFClient\Support\Utility;
use N1ebieski\KSEFClient\Testing\Fixtures\Requests\Online\Query\Invoice\Async\Init\InitRequestFixture;
use N1ebieski\KSEFClient\ValueObjects\Mode;
$encryptionKey = EncryptionKeyFactory::makeRandom();
$client = new ClientBuilder()
->withMode(Mode::Test)
->withApiToken($_ENV['KSEF_KEY'])
->withKSEFPublicKeyPath(__DIR__ . '/../config/keys/publicKey.pem')
->withEncryptionKey($encryptionKey)
->build();
Utility::retry(function () use ($client) {
$statusResponse = $client->online()->session()->status()->object();
if ($statusResponse->processingCode === 315) {
return $statusResponse;
}
});
// Firstly we need to init the preparation of the invoice package based on the query parameters
$initResponse = $client->online()->query()->invoice()->async()->init(
new InitRequestFixture()->withRange('-2 weeks')->withSubjectType('subject1')->data
)->object();
// Preparing invoice packs is asynchronous so it's better to save SessionToken
// and queryElementReferenceNumber, go for coffee and come back in a while :)
$sessionToken = $client->getSessionToken();
$queryElementReferenceNumber = $initResponse->elementReferenceNumber;
// Ok after a few minutes...
$client = new ClientBuilder()
->withMode(Mode::Test)
->withSessionToken($sessionToken)
->withKSEFPublicKeyPath(__DIR__ . '/../config/keys/publicKey.pem')
->withEncryptionKey($encryptionKey)
->build();
// Check if packages are ready to download
$statusResponse = Utility::retry(function () use ($client, $queryElementReferenceNumber) {
$statusResponse = $client->online()->query()->invoice()->async()->status([
'queryElementReferenceNumber' => $queryElementReferenceNumber
])->object();
if ($statusResponse->processingCode === 200) {
return $statusResponse;
}
});
// Downloading...
foreach ($statusResponse->partList as $part) {
$fetchResponse = $client->online()->query()->invoice()->async()->fetch([
'queryElementReferenceNumber' => $queryElementReferenceNumber,
'partElementReferenceNumber' => $part->partReferenceNumber
])->body();
file_put_contents(__DIR__ . "/../var/zip/{$part->partReferenceNumber}.zip", $fetchResponse);
}
$client->online()->session()->terminate();
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.