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

Example sharing of php extension kafka under linux

小云云
Release: 2018-05-11 15:11:36
Original
2637 people have browsed it

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 #安装
Copy after login

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
Copy after login

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

extension="kafka.so"
Copy after login

:wq! #Save and exit

4. Test

The following code is saved as phpinfo.php

<?php
phpinfo();
?>
Copy after login

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);
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template