PHP code example of linwanfeng / kafka-php-sdk

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

    

linwanfeng / kafka-php-sdk example snippets



use kafkaPhp\KafkaProducer;
use kafkaPhp\KafkaException;
try{
    $kafkaClient = new KafkaProducer('127.0.0.1:9092');
    $kafkaClient->sendMsg('topic', 'msg');
}catch (KafkaException $e){
    echo $e->getErrorMessage();die;
}


use kafkaPhp\KafkaSkProducer;
use kafkaPhp\KafkaException;
try{
   $kafkaClient = new KafkaSkProducer('***.***.*.***', '***');
   $response = $kafkaClient->sendMsg('topic', 'msg');
}catch (KafkaException $e){
    echo $e->getErrorMessage();
}


kafkaPhp\KafkaClient;
use kafkaPhp\KafkaException;

try{
    $kafkaClient = new KafkaClient('127.0.0.1:9092', 1000);
    $kafkaClient->initConsumer(['topic1','topic2'], 1);
    while(true){
        $r = $kafkaClient->getMsg(2, 1000);
        ...
    }

}catch (KafkaException $e){
    echo $e->getErrorMessage();die;
}