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 #安装
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
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('testkk'); $in = fopen('php://stdin', 'r'); while (true) { echo "\nEnter comma separated messages:\n"; $messages = explode(',', 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 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!