Getting Started with Redpanda using PHP

Special Guest Nick Chiu shares his view on integrating PHP with Redpanda

By Nick Chiu on April 29, 2021

Time to read icon7 min read

In this blog, I’m [Nick Chiu] going to show you how to set up Redpanda and use it with PHP.
We will build a very simple producer and consumer example in a Docker setup.
This is an introduction to Redpanda, so if you already have experience with it
or similar streaming platforms, you can check out some of our posts on more advanced topics,
like this one on consistency, or this one on autotuning.

As you may have heard by now, Redpanda is Kafka® API-compatible,
which means that although Redpanda is relatively new, you can leverage the countless client libraries
created for Kafka® (if you find something that is not supported, reach out to our team on our slack).
In this case we will use the PHP extension simple_kafka_client.

Prepare Docker setup

First create a Dockerfile for our PHP container:

FROM php:8.0-cli-alpine3.13
# Install packages
RUN apk --no-cache add bash gcc g++ make autoconf && \
apk add librdkafka librdkafka-dev \
--update-cache --repository && \
pecl install simple_kafka_client && \
docker-php-ext-enable simple_kafka_client

Then let’s create our docker-compose.yml like follows:

version: '3.7'
context: ./
tty: true
working_dir: /src
- ./src:/src
- /usr/bin/rpk
- redpanda
- start
- --smp
- '1'
- --reserve-memory
- 0M
- --overprovisioned
- --node-id
- '0'
- --kafka-addr
- --advertise-kafka-addr
- PLAINTEXT://redpanda:29097,OUTSIDE://redpanda:9097
- --check=false
image: vectorized/redpanda:v21.4.12
- 9097:9097
- 29097:29097

Create the PHP code examples

Next we create the src folder with our examples.
This folder will be mounted into our Docker setup.

mkdir src;cd src

Then let’s create our producer.php in the src folder with the following content:

use SimpleKafkaClient\Configuration;
use SimpleKafkaClient\Message;
use SimpleKafkaClient\Producer;
$conf = new Configuration();
$conf->set('', 'redpanda:9097');
$conf->set('compression.codec', 'zstd');
$producer = new Producer($conf);
$topic = $producer->getTopicHandle('php-test-topic');
for ($i = 0; $i < 10; ++$i) {
RD_KAFKA_MSG_F_BLOCK, // will block produce if queue is full
sprintf('test message-%d',$i),
sprintf('test-key-%d', $i),
'some' => sprintf('header value %d', $i)
$result = $producer->flush(20000);
echo sprintf('Produced %d messages', $i) . PHP_EOL;
if (RD_KAFKA_RESP_ERR_NO_ERROR !== $result) {
echo 'Was not able to shutdown within 20s. Messages might be lost!' . PHP_EOL;

Next let’s create our consumer.php in the src folder with the following content:

use SimpleKafkaClient\Configuration;
use SimpleKafkaClient\Consumer;
$conf = new Configuration();
$conf->set('', 'php-consumer');
$conf->set('', 'redpanda:9097');
$conf->set('auto.offset.reset', 'earliest');
$conf->set('enable.partition.eof', 'true');
$consumer = new Consumer($conf);
while (true) {
$message = $consumer->consume(20000);
if (RD_KAFKA_RESP_ERR__PARTITION_EOF === $message->err) {
echo 'Reached end of partition, shutting down' . PHP_EOL;
} else if (RD_KAFKA_RESP_ERR__TIMED_OUT === $message->err) {
echo 'Timed out without receiving a new message, waiting for more messages...' . PHP_EOL;
} else if (RD_KAFKA_RESP_ERR_NO_ERROR !== $message->err) {
echo kafka_err2str($message->err) . PHP_EOL;
echo sprintf(
'Read message with key:%s payload:%s topic:%s partition:%d offset:%d',
) . PHP_EOL;

Running the examples

Now let’s put it all together and run our examples:

docker-compose up -d
docker-compose exec php php producer.php
docker-compose exec php php consumer.php

Wrapping up

As you can see, it is a breeze to setup PHP integration with Redpanda.
Try it yourself, there are endless use cases - what we built here was just the simplest of examples.

by Special Guest, Nick Chiu @thr33One

Join our Redpanda Slack Community.

Related articles