PHP code example of anwarme / kafka-php

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

    

anwarme / kafka-php example snippets




use RdKafka\Message;

$kafka = new Kafka\KafkaPhp("localhost:9092");
$kafka->topic = 'v3-domainaggregate';
//$kafka->produce(payload: json_encode(['data empty']), headers: ['whitelist_label' => '$whitelist_label']);

[$low, $high] = $wmoffsets = $kafka->watermarkOffsets();
echo "topic starting offset: $low, ending offset(EOF): $high\n";
echo "current offset position: " . $kafka->getCommittedOffset() . "\n";
/** @var Message[] $messages */
[$messages, $eof, $from_offset, $to_offset] = $m = $kafka->getMessages(0, RD_KAFKA_OFFSET_BEGINNING);
echo("count fetched, EOF, fetched start offset, fetched last offset: " . json_encode([count($messages), $eof, $from_offset, $to_offset]) . "\n");

if (count($messages)) {
    echo "message headers: " . json_encode($messages[0]->headers) . "\n";
}

$committedOffset = $kafka->getCommittedOffset();
echo "current offset position: $committedOffset\n";

apt install php-rdkafka
composer