Windows
PHP XAMPP
librdkafka
Installation guide
Programming

Installing librdkafka on Windows for PHP XAMPP

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Librdkafka is a C client library for Apache Kafka that is used by a variety of language-specific clients, including PHP. PHP developers who utilize the XAMPP environment on Windows might find it challenging to integrate Kafka due to the dependency on native libraries such as librdkafka. This article will guide you step by step on how to install librdkafka and utilize it within a PHP XAMPP environment on a Windows system.

Prerequisites

Before starting the installation, ensure that you have the following:

  • XAMPP for Windows: A free and open-source cross-platform web server solution that consists mainly of the Apache HTTP Server, MariaDB database, and interpreters for PHP and Perl scripts.
  • Git for Windows: Useful for fetching some required repositories.
  • Microsoft Visual C++ Build Tools: Required for some compilations steps.

Step 1: Installing librdkafka

The first step in using librdkafka with PHP on Windows is to compile or obtain pre-built binaries of librdkafka. Here’s how you can compile it:

Compiling librdkafka

  1. Install Build Tools:
    • Download and install "Visual Studio Build Tools" specifically ensuring that you include C++ build tools in the installation.
  2. Clone librdkafka GitHub Repository:
    • Open Git Bash and use the following command to clone the repository:
bash
   git clone https://github.com/edenhill/librdkafka.git
  1. Build The Library:
    • Open Visual Studio Command Prompt or a regular command prompt where you can utilize the MSVC compiler.
    • Navigate to the cloned librdkafka directory.
    • Run the following command:
bat
   .\configure
   nmake

Once building is complete, librdkafka.dll will be generated in the src directory.

Step 2: PHP Extensions for Kafka

PHP does not come with native support for Kafka, so we need to use an extension. The most commonly used one is php-rdkafka.

Installing php-rdkafka

  1. Download the PHP Extension:
    • Go to PECL and download the latest php_rdkafka.dll that matches your PHP version and architecture (e.g., x86, x64).
  2. Configure Your PHP Environment:
    • Include your librdkafka.dll in your PATH or add it to your XAMPP php directory.
    • Add the php_rdkafka.dll to the ext directory of your XAMPP installation.
    • Modify your php.ini (within the XAMPP control panel, you can click on Config -> PHP(php.ini)), and add the following extension entry:
ini
   extension=php_rdkafka.dll

Step 3: Testing The Installation

To test the Kafka setup, you can create a simple PHP script that produces and consumes messages:

php
1<?php
2// Produce a message
3$conf = new RdKafka\Conf();
4$conf->set('metadata.broker.list', 'localhost:9092');
5
6$producer = new RdKafka\Producer($conf);
7$topic = $producer->newTopic("test");
8
9$topic->produce(RD_KAFKA_PARTITION_UA, 0, "Message payload");
10$producer->poll(0);
11
12for ($flushRetries = 0; $flushRetries < 10; $flushRetries++) {
13    $result = $producer->flush(10000);
14    if (RD_KAFKA_RESP_ERR_NO_ERROR === $result) {
15        break;
16    }
17}
18
19// Consume a message
20$conf = new RdKafka\Conf();
21$conf->set('group.id', 'myConsumerGroup');
22$rk = new RdKafka\Consumer($conf);
23$rk->addBrokers("localhost:9092");
24
25$topic = $rk->newTopic("test");
26$topic->consumeStart(0, RD_KAFKA_OFFSET_BEGINNING);
27$message = $topic->consume(0, 120*10000);
28var_dump($message);
29?>

Key Points Summary

FeatureDetails
Librarylibrdkafka (C Apache Kafka client)
PHP Extensionphp-rdkafka
XAMPP CompatibilityCompatible with setup instructions
Development PrerequisitesVisual C++ Build Tools, PHP development environment, Git

Conclusion

Installing librdkafka on Windows for use with PHP under XAMPP involves several steps from compiling the native Kafka C client to correctly installing and configuring the PHP extension. Careful attention is needed to ensure the environments align (such as 32-bit vs. 64-bit) and that all dependencies are correctly placed and declared in the configuration files. With the steps outlined above, developers can set up a PHP-driven consumer or producer of Kafka events in their local XAMPP development environment.


Course illustration
Course illustration

All Rights Reserved.