PHP code example of liangbc / kafka

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

    

liangbc / kafka example snippets


//  Initialize kafkaConsume configuration
$consumerConfig = new \kafka\ConsumeConfig('127.0.0.1:9902,127.0.0.1:9903');

//  Create low-level consumer instance
//$consumer = new \kafka\LowConsumer($kafkaConfig);

//  Create high-level consumer instance
$consumer = new \kafka\SeniorConsumer($consumerConfig);

//  Manually submit the offset
//$consumer->autoCommit = true;

//  自动提交超时自动提交时间,毫秒
//$consumer->autoCommitIntervalMs = 5 * 1000;

//  Consumer subscription message
$consumer->consumer(['topic1', 'topic2'], function (\RdKafka\Message $message, \RdKafka\KafkaConsumer $consumer) {
    var_dump($message->payload);

    //  Manually submit the offset
    $consumer->commit();
});