Home >Operation and Maintenance >Linux Operation and Maintenance >Example sharing of php extension kafka under linux

Example sharing of php extension kafka under linux

小云云
小云云Original
2018-05-11 15:11:362679browse

This article mainly shares with you examples of php extension kafka under Linux, hoping to help everyone.

1. Install librdkafka

wget https://github.com/edenhill/librdkafka/archive/master.zip #下载
mv master.zip librdkafka-master.zip #修改包名
unzip librdkafka-master.zip #解压
cd librdkafka-master #进入安装文件夹
./configure #配置
make #编译
make install #安装

2. Install phpkafka

cd /usr/local/src #进入安装包存放目录
wget https://github.com/EVODelavega/phpkafka/archive/master.zip #下载
mv master.zip phpkafka-master.zip #修改包名
unzip phpkafka-master.zip #解压
cd phpkafka-master #进入安装文件夹
/usr/local/php/bin/phpize #加载php扩展模块
./configure --enable-kafka --with-php-config=/usr/local/php/bin/php-config #配置
make #编译
make install #安装
3、修改php配置文件
vi /usr/local/php/etc/php.ini

Open the php configuration file and add the following code in the last line

extension="kafka.so"

:wq! #Save and exit

4. Test

The following code is saved as phpinfo.php

<?php
phpinfo();
?>

There is kafka module

This is the producer

$kafka = new Kafka("localhost:9092");
$partitions = $kafka->getPartitionsForTopic(&#39;testkk&#39;);
$in = fopen(&#39;php://stdin&#39;, &#39;r&#39;);
while (true) {
    echo "\nEnter comma separated messages:\n";
    $messages = explode(&#39;,&#39;, fgets($in));
    foreach (array_keys($messages) as $k) {
        //$messages[$k] = trim($messages[$k]);
    }
        $bytes=$kafka->produce("testkk", "kkkkkkk");
    printf("\nSuccessfully sent %d messages (%d bytes)\n\n", count($messages), $bytes);
}

Related recommendations:

Kafka client written in PHP

kafka installation and use of Kafka-PHP extension

Usage of kafka assembly and Kafka-PHP extension

The above is the detailed content of Example sharing of php extension kafka under linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn