PHP code example of grongor / kafka-rest-client

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

    

grongor / kafka-rest-client example snippets


$producer = new Producer($restClient);
$producer->produce('your-topic', new Message('some-message'));

$messages = [new Message('some-message'), new Message('and-some-other-message')];
$producer->produceBatch('your-topic', $messages);

$consumerFactory = new ConsumerFactory($restClient);
$consumer = $consumerFactory->create('your-consumer-group', Subscription::topic('your-topic'));
foreach ($consumer->consume() as $message) {
    // Do your magic
    $logger->info('Got new message', $message->content);

    // ... and when you are done, commit the message (if you turned off auto-committing).
    $consumer->commit($message);
}

$batchConsumerFactory = new BatchConsumerFactory($restClient);
$batchConsumer = $batchConsumerFactory->create(
    'your-consumer-group',
    Subscription::topic('your-topic'),
    $maxCount = 10000, // Yield the batch when there is 10 000 messages in it
    $maxDuration = 60  // or when 60 seconds passes, whichever comes first.
);
foreach ($batchConsumer->consume() as $messagesBatch) {
    // The batch might be empty if you specified the maxDuration.
    if ($messagesBatch->isEmpty()) {
        continue;
    }

    // Do your magic
    $database->insert($messagesBatch->getMessages());

    // ... and when you are done, commit the batch.
    $batchConsumer->commit($messagesBatch);
}