PHP code example of arquivei / php-kafka

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

    

arquivei / php-kafka example snippets




afka\Consumer\Entities\Config;
use Kafka\Consumer\Contracts\ConsumerDLQ;
use Kafka\Consumer\Entities\Config\Sasl;
use Kafka\Consumer\Entities\Config\MaxAttempt;

class DefaultConsumer extends Consumer
{
    public function handle(string $message): void
    {
        print 'Init: ' . date('Y-m-d H:i:s') . PHP_EOL;
        sleep(2);
        print 'Finish: ' . date('Y-m-d H:i:s') . PHP_EOL;
    }
}

$config = new Config(
    new Sasl('username', 'pasword', 'mechanisms'),
    'topic',
    'broker:port',
    1,
    'php-kafka-consumer-group-id',
    new DefaultConsumer(),
    'security-protocol'
);

(new \PHP\Kafka\Consumer($config))->consume();



$config = new Config(
    new Sasl('username', 'pasword', 'mechanisms'),
    'topic',
    'broker:port',
    1,
    'php-kafka-consumer-group-id',
    new DLDConsumer(),
    new DLQ("parking-lot"),
    'security-protocol'
);

(new \PHP\Kafka\Consumer($config))->consume();