PHP code example of xiazhengxin / php_rd_kafka_wrapper
1. Go to this page and download the library: Download xiazhengxin/php_rd_kafka_wrapper library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
xiazhengxin / php_rd_kafka_wrapper example snippets
//通过静态配置方法获取 生产者
$config = ["xxx" => "yyy"];
$kafkaConfig = \TPUtil\MQ::getKafkaConfig($config);
$producer = \TPUtil\MQ::getProducerByConfig($kafkaConfig);
$pTopic = $producer->newTopic("test");
$pTopic->produce(0, 0, "hehe");
//通过静态方法获取 消费者主题
$topic = \TPUtil\MQ::getConsumer()->newTopic("test");
//使用消费者主题实例 填充生成 MQ包装类 对象
$mq = new \TPUtil\MQ($topic);
//开始轮询RdKafka 主题内的消息队列
$mq->pullAllFromIt(function ($msg, $err) {
var_dump($msg);// \RdKafka\Message 对象
var_dump($err);// 错误信息
}, 0, 60 * 60 * 24 * 30 * 1000);
//停止轮询
$mq->stopPull();
$ php composer.phar update xiazhengxin/php_rd_kafka_wrapper