PHP code example of christian-heiko / bka-api

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

    

christian-heiko / bka-api example snippets




use ChristianHeiko\Bka\Data\Text;
use ChristianHeiko\Bka\Enum\EventStatus;
use ChristianHeiko\Bka\V2\Client;
use ChristianHeiko\Bka\V2\Data\Event;
use ChristianHeiko\Bka\V2\Data\Rate;
use ChristianHeiko\Bka\V2\Data\Ticketing;

$client = new Client('https://admin.bka.ch/api', $token);
// or: Client::makeFromEnv(); // BKA_URL + BKA_TOKEN

$zurich = new DateTimeZone('Europe/Zurich');

$event = new Event(
    'Some Party Title',
    EventStatus::confirmed,
    'your-place-slug',   // places are addressed by slug — see $client->places('keyword')
    [1, 3],              // category ids — $client->categories()
    new DateTime('2026-12-31 22:00', $zurich),
    new DateTime('2027-01-01 04:00', $zurich),
    Text::make('de', '<p>Best Party in Town</p>'),
    1,                   // audience id — $client->audiences()
    openingTime: new DateTime('2026-12-31 21:30', $zurich),
);

$event->attachRate(new Rate(25, Text::make('de', 'Normal')));
$event->attachTicketing(Ticketing::fromUrl('https://www.petzi.ch/de/events/1', Text::make('de', 'Vorverkauf')));
$event->attachImage($client->uploadImage('/path/to/image.jpg')->id);

$saved = $client->saveEvent($event, $knownSlug); // PATCH by slug, or create when none is known
// Keep $saved->slug: it changes when the event is renamed.

$client->events(page: 1, itemsPerPage: 30);   // the account's published events; lastTotal() has the count
$client->allEvents();
$client->event('slug');                        // null when not found — or a draft, see below
$client->createEvent($event);
$client->updateEvent('slug', $event);          // JSON merge patch
$client->saveEvent($event, 'slug' | null);
$client->deleteEvent('slug');
$client->uploadImage('/path/or/url.jpg');
$client->deleteImage($id);
$client->categories(); $client->audiences(); $client->affiliations(); $client->regions();
$client->places(keyword: 'bern'); $client->organizations(keyword: 'bern');



use ChristianHeiko\Bka\Client

$client = new Client(
    'url',
    'clientId',
    'clientSecret',
    'accessToken',
    'refreshToken'
);



use ChristianHeiko\Bka\Client
$accessToken = 'Load From DB or where-ever';
$client = Client::makeFromEnv($accessToken);



use ChristianHeiko\Bka\Client;

$client = new Client(
    'url', 'clientId', 'clientSecret', 'accessToken', 'refreshToken',
    onTokenRefresh: function (string $accessToken, string $refreshToken): void {
        // Persist these; pass the access token back in on the next request.
    }
);

// Also available on the .env constructor:
$client = Client::makeFromEnv($accessToken, onTokenRefresh: $callback);



use ChristianHeiko\Bka\Client

$client = Client::makeFromEnv('...');

$client->events(date_from: new DateTime(), free: true);
$client->event('slug-to-event');
$client->addEvent(['data for event']);
$client->updateEvent('id', ['data for event']);
$client->saveEvent(['data for event'], 'id'|null); // Shortcut to add/update
$client->deleteEvent('id');

$client->sponsoredEvents();
$client->similarEvents('slug-to-event');
$client->eventsForOrganization('slug-to-organization');
$client->eventsForPlace('slug-to-place');

$client->affiliations();
$client->audiences();
$client->categories();
$client->organizations(keyword: 'bern', limit: 10);
$client->organization('slug-to-organization');
$client->places(keyword: 'bern', limit: 10, place: 1);
$client->place('slug-to-place');
$client->regions();



$page = $client->events(limit: 50, page: 1);

$client->lastTotal();     // e.g. 874 — total records, or null if the endpoint reports none
$client->lastEnvelope;    // the full envelope: code, message, data, total



use ChristianHeiko\Bka\Exception\AuthenticationException;
use ChristianHeiko\Bka\Exception\BkaException;
use ChristianHeiko\Bka\Exception\InvalidResponseException;
use GuzzleHttp\Exception\ClientException;

try {
    $client->saveEvent($eventData);
} catch (AuthenticationException $e) {
    // The refresh token is expired or revoked — the user has to re-authorise.
} catch (InvalidResponseException $e) {
    // The API replied with something unparseable, e.g. a proxy error page.
} catch (ClientException $e) {
    // A 4xx from the API itself. Read the body for the validation errors:
    var_dump($client->json($e->getResponse()));
} catch (BkaException $e) {
    // Anything else originating from this package.
}



$client = new Client(
    'url', 'clientId', 'clientSecret', 'accessToken', 'refreshToken',
    guzzleOptions: ['timeout' => 5.0, 'proxy' => 'tcp://proxy.example:8080']
);