PHP code example of euskadi31 / kafka-php

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

    

euskadi31 / kafka-php example snippets




$cm = new Euskadi31\Kafka\ClusterMetadata\Zookeeper([
    '10.0.0.13' => 2181
]);

$producer = new Euskadi31\Kafka\Producer($cm);

$topic = $producer->newTopic('test');

$topic->produce(RD_KAFKA_PARTITION_UA, 0, 'Message payload');



$cm = new Euskadi31\Kafka\ClusterMetadata\Zookeeper([
    '10.0.0.13' => 2181
]);

$conf = new RdKafka\Conf();

$consumer = new Euskadi31\Kafka\Consumer($cm, $conf);

$topic = $consumer->newTopic('test');

$topic->consumeStart(0, RD_KAFKA_OFFSET_BEGINNING);

while (true) {
    // The first argument is the partition (again).
    // The second argument is the timeout.
    $msg = $topic->consume(0, 1000);

    if (empty($msg)) {
        continue;
    }

    if ($msg->err) {
        echo $msg->errstr() . PHP_EOL;
        break;
    } else {
        echo $msg->payload . PHP_EOL;
    }
}