Kafka Extension
PHP Guide
Software Installation
Programming Tutorial
Apache Kafka

How do I install an extension of Kafka for PHP?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Apache Kafka is a popular distributed event streaming platform capable of handling trillions of events a day. Integrating Kafka with different programming languages can expand its utility. For PHP developers, the php-rdkafka extension is a high-quality PHP extension that provides a stable and performant interface to the Apache Kafka cluster.

Introduction to php-rdkafka

The php-rdkafka extension is a Kafka client for PHP, based on the librdkafka C library, which provides low-level functionality necessary for Kafka communication. This extension supports the advanced features of Kafka (like message compression and custom partitioners) and can be a useful tool for PHP applications that require integration with Kafka for event-driven architectures.

Prerequisites

Before you install php-rdkafka, ensure you have the following prerequisites installed:

  • PHP development environment (PHP 7.0 or greater)
  • php-pear and php-dev packages to compile and install PHP extensions
  • build-essential for required tools like make
  • librdkafka (the C library that powers the extension)

The version of librdkafka should be compatible with the php-rdkafka version you intend to install.

Step-by-Step Installation Guide

1. Install librdkafka

First, you need to install librdkafka, as it is a required dependency for php-rdkafka. You can install it from source or from pre-built binaries. Here is how you can install it from source:

bash
1git clone https://github.com/edenhill/librdkafka.git
2cd librdkafka
3./configure
4make
5sudo make install

2. Install php-rdkafka

Once librdkafka is installed, you can proceed to install the php-rdkafka extension. This can also be done in several ways, including PECL, compiling from source, or using a package manager if available.

Installing via PECL

PECL is the simplest method if available:

bash
pecl install rdkafka

If successful, add extension=rdkafka.so to your php.ini file.

Compiling from Source

If you prefer or need to compile manually from source, here are the steps:

bash
1git clone https://github.com/arnaud-lb/php-rdkafka.git
2cd php-rdkafka
3phpize
4./configure
5make
6sudo make install

Add extension=rdkafka.so to your php.ini file.

3. Verify the Installation

To verify that the installation was successful, run:

bash
php --ri rdkafka

This command should display the version of rdkafka and the available configuration options.

Usage Example

With php-rdkafka installed, you can now produce and consume messages with Kafka. Here is a simple example of producing a message:

php
1<?php
2$conf = new RdKafka\Conf();
3$conf->set('metadata.broker.list', 'localhost:9092');
4
5$producer = new RdKafka\Producer($conf);
6$topic = $producer->newTopic("test");
7
8$topic->produce(RD_KAFKA_PARTITION_UA, 0, "Message payload");

Summary

Here’s a table summarizing the key steps and components involved in setting up php-rdkafka with PHP:

StepComponentPurpose
Install prerequisitesPHP, php-pear, etc.Prepare environment for php-rdkafka
Install librdkafkalibrdkafkaC library dependency for php-rdkafka
Install php-rdkafka via PECL or sourcephp-rdkafkaPHP extension for Apache Kafka
Verify installationphp --ri rdkafkaEnsure correct installation
Test with a producer code snippetKafka producerTest producing messages to Kafka

Conclusion

Integrating Kafka with PHP using the php-rdkafka extension can significantly enhance an application's capabilities to interact with real-time data streams. With proper installation and configuration, PHP applications can both produce to and consume from a Kafka cluster, opening up a wide range of possibilities for real-time data processing and analytics.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.