PHP code example of alpari / kafka-client

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

    

alpari / kafka-client example snippets


use Alpari\Kafka\DTO\Message;
use Alpari\Kafka\Producer\Config;
use Alpari\Kafka\Producer\KafkaProducer;

ducer->send('test', Message::fromValue('foo'));

use Alpari\Kafka;
use Alpari\Kafka\Consumer\Config;
use Alpari\Kafka\Consumer\KafkaConsumer;

$consumer = new KafkaConsumer([
    Config::BOOTSTRAP_SERVERS       => ['tcp://localhost'],
    Config::GROUP_ID                => 'Kafka-Daemon',
    Config::FETCH_MAX_WAIT_MS       => 5000,
    Config::AUTO_OFFSET_RESET       => Kafka\Consumer\OffsetResetStrategy::LATEST,
    Config::SESSION_TIMEOUT_MS      => 30000,
    Config::AUTO_COMMIT_INTERVAL_MS => 10000,
    Config::METADATA_CACHE_FILE     => '/tmp/metadata.php',
]);
$consumer->subscribe(['test']);
for ($i=0; $i<100; $i++) {
    $data = $consumer->poll(1000);
    echo json_encode($data), PHP_EOL;
}