Connect to Aiven for Apache Kafka® with PHP
Use Quick connect to set up a PHP client for Aiven for Apache Kafka®. The guided flow helps you choose or create a topic, choose an authentication method, grant permissions, and copy generated producer and consumer code.
Prerequisites
- A running Aiven for Apache Kafka® service.
- PHP 8.3 or later.
- The
rdkafkaPECL extension, which requires thelibrdkafkasystem library.
For SASL/SCRAM authentication with Apache Kafka® 4.x, use a php-rdkafka
version built on librdkafka version 2.6.1 or later. For more information, see
Enable and configure SASL authentication for Apache Kafka®.
Open Quick connect
- In the Aiven Console, open your Aiven for Apache Kafka service.
- On the service Overview page, in the Set up your stream section, click Quick connect.
- At the top of the page, click PHP.
Step 1: Set up a topic
Topics organize and store the events that you stream to Apache Kafka.
-
In Topic name, do one of the following:
- Choose an existing topic.
- Click Create new topic, enter a name, and create the topic. The new topic is selected automatically for the next steps.
noteIf your service has Diskless topics enabled, you can create a Classic or Diskless topic, or choose an existing topic of either type. You can't change the topic type after creation. For more information, see Create Apache Kafka® topics.
Step 2: Set up an authentication method
-
Choose an authentication method:
-
SASL: Recommended. Use SASL/SCRAM-SHA-256 for username and password authentication.
SASL is enabled by default for new Aiven for Apache Kafka services. For existing services, verify whether SASL is enabled. If Enable SASL appears, click Enable SASL, then continue.
-
Client certificate: Use certificate-based authentication with mTLS instead of a password. Downloads include a CA certificate, service certificate, and access key.
-
-
Choose a service user:
- Choose an existing service user from the list.
- To create one, click Create new service user, enter a username, and click Add service user.
-
Check the permission status shown for the selected user:
- If the user has all the required permissions, the granted permissions are
shown (for example,
read, write). To change them, click Manage access in ACLs. - If the user has some or no permissions, click Grant permissions. Choose Produce, Consume, or both, and click Save.
noteThe
avnadminuser has permissions by default.For other service users, you can add permissions from Quick connect. To change or remove permissions, click Manage access in ACLs. For more information, see Manage Apache Kafka® ACLs.
- If the user has all the required permissions, the granted permissions are
shown (for example,
Step 3: Copy the code snippets
Download template is an optional shortcut for testing the connection. The
ZIP file includes ready-to-run producer and consumer code, certificates, and a
Containerfile based on php:8.3-cli. To run it, follow the included
README.md. If you already have a PHP project, copy the snippet from the
Producer or Consumer tab.
-
Install
librdkafka. For installation instructions, seelibrdkafkaon GitHub. -
Install the
rdkafkaPECL extension:pecl install rdkafka -
If the extension is not already enabled, add
extension=rdkafka.soto yourphp.inifile.The generated examples require PHP CLI with the
pcntlextension enabled for signal handling. -
Under Downloads, download the certificate files for your authentication method:
- For SASL, click Download CA certificate.
- For Client certificate, click Download CA certificate, Download service certificate, and Download service access key.
The generated snippet loads the certificates directly. You don't need to create a truststore.
-
Click the Producer or Consumer tab to view the generated producer or consumer code.
-
Copy the code into
producer.phporconsumer.php.warningFor SASL, copied snippets include the service user password in plaintext. Store the code securely, and do not commit it to source control.
After you add the code to your project, update the certificate file paths to match where you saved the files, then run your producer or consumer to start streaming events.