This article mainly introduces the method of using PHP to access the RabbitMQ message queue, and analyzes the related extension installation, queue establishment, queue binding, message sending, message receiving and other related operation skills of the RabbitMQ message queue in the form of examples. The required Friends can refer to
This article describes how to use PHP to access the RabbitMQ message queue. Share it with everyone for your reference, the details are as follows:
Extension installation
PHP actually uses the AMQP protocol to access RabbitMQ, so we only need to install php- in the epel library pecl-amqp This package can
rpm -ivh http://mirror.neu.edu.cn/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm yum install php-pecl-amqp
exchange establishment
<?php $connection = new AMQPConnection(); $connection->connect(); $channel = new AMQPChannel($connection); $exchange = new AMQPExchange($channel); $exchange->setName('exchange1'); $exchange->setType('fanout'); $exchange->declare();
Queue establishment
<?php $connection = new AMQPConnection(); $connection->connect(); $channel = new AMQPChannel($connection); $queue = new AMQPQueue($channel); $queue->setName('queue1'); $queue->declare();
Queue binding
<?php $connection = new AMQPConnection(); $connection->connect(); $channel = new AMQPChannel($connection); $queue = new AMQPQueue($channel); $queue->setName('queue1'); $queue->declare(); $queue->bind('exchange1', 'routekey');
Message sent
<?php $connection = new AMQPConnection(); $connection->connect(); $channel = new AMQPChannel($connection); $exchange = new AMQPExchange($channel); $exchange->setName('exchange5'); $exchange->setType('fanout'); $exchange->declare(); for($i = 0; $i < 2000000; $i++) { $exchange->publish("message $i", "routekey"); }
Message received
<?php $connection = new AMQPConnection(); $connection->connect(); $channel = new AMQPChannel($connection); $queue = new AMQPQueue($channel); $queue->setName('queue1'); $queue->declare(); $queue->bind('exchange1', 'routekey'); while (true) { $queue->consume(function($envelope, $queue){ echo $envelope->getBody(), PHP_EOL; }, AMQP_AUTOACK); }
Related recommendations:
The above is the detailed content of How to access RabbitMQ message queue using PHP. For more information, please follow other related articles on the PHP Chinese website!