PHP Kafka CDC is a package developed by Fatih Tuzlu for managing multidata with Apache Kafka in PHP applications. It provides functionalities for both consuming and producing messages using Kafka.
Do not forget to install rdkafka library
brew install librdkafka
$ apt install librdkafka-dev
$ yum install librdkafka-devel
# Install vcpkg if not already installed
$ git clone https://github.com/Microsoft/vcpkg.git
$ cd vcpkg
$ ./bootstrap-vcpkg.sh
$ ./vcpkg integrate install
# Install librdkafka
$ vcpkg install librdkafka
You can visit https://github.com/confluentinc/librdkafka
You can install PHP Kafka CDC via Composer. Run the following command in your terminal:
composer require fatihtuzlu/php-apache-kafka
PHP Kafka CDC requires the following dependencies:
idealo/php-rdkafka-ffi: ^0.5.0
php: ^8.0.0
To consume messages from Kafka topics, you can use the ConsumerManager class. Here's a basic example of how to set up a consumer:
use Fatihtuzlu\PHPKafkaCDC\ConsumerManager;
// Initialize consumer manager
$consumer = new ConsumerManager(
$topic, // Kafka topic to consume from
$groupId, // Consumer group ID
$bootstrapServers // Kafka bootstrap servers
);
// Fetch messages from Kafka
$message = $consumer->fetchMessages($topic, $partition, $offset, $timeMs);
To produce messages to Kafka topics, you can use the ProducerManager class. Here's a basic example of how to set up a producer:
use Fatihtuzlu\PHPKafkaCDC\ProducerManager;
// Initialize producer manager
$producer = new ProducerManager(
$topic, // Kafka topic to produce to
$bootstrapServers // Kafka bootstrap servers
);
// Send a message to Kafka
$producer->sendMessages($message, $flush = 1000, $debug = false);
You can enable debugging for your Kafka connection by calling the debug() method:
$producer->debug();
$producer->addBrokers($brokerList);
use Fatihtuzlu\PHPKafkaCDC\ConsumerManager;
// Kafka broker and topic information
$bootstrapServers = "localhost:9092";
$topic = "my_topic";
$groupId = "my_group";
// Initialize the consumer
$consumer = new ConsumerManager($topic, $groupId, $bootstrapServers);
// Fetch messages
$message = $consumer->fetchMessages($topic, 0, RD_KAFKA_OFFSET_BEGINNING);
// Process the received message
if ($message !== null) {
echo "Received message: " . $message->payload . "\n";
} else {
echo "No messages available.\n";
}
use Fatihtuzlu\PHPKafkaCDC\ProducerManager;
// Kafka broker and topic information
$bootstrapServers = "localhost:9092";
$topic = "my_topic";
// Initialize the producer
$producer = new ProducerManager($topic, $bootstrapServers);
// Send a message
$messageToSend = "Hello, Kafka!";
$producer->sendMessages($messageToSend);
echo "Message sent to Kafka: " . $messageToSend . "\n";
In these examples, the consumer retrieves messages from Kafka, while the producer sends messages to Kafka. my_topic represents the target topic in Kafka. Make sure to update the configuration settings according to your own Kafka cluster.