Home > Backend Development > PHP Tutorial > How to access RabbitMQ message queue using PHP

How to access RabbitMQ message queue using PHP

不言
Release: 2023-03-30 10:42:02
Original
2551 people have browsed it

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

exchange establishment

<?php
$connection = new AMQPConnection();
$connection->connect();
$channel = new AMQPChannel($connection);
$exchange = new AMQPExchange($channel);
$exchange->setName(&#39;exchange1&#39;);
$exchange->setType(&#39;fanout&#39;);
$exchange->declare();
Copy after login

Queue establishment

<?php
$connection = new AMQPConnection();
$connection->connect();
$channel = new AMQPChannel($connection);
$queue = new AMQPQueue($channel);
$queue->setName(&#39;queue1&#39;);
$queue->declare();
Copy after login

Queue binding

<?php
$connection = new AMQPConnection();
$connection->connect();
$channel = new AMQPChannel($connection);
$queue = new AMQPQueue($channel);
$queue->setName(&#39;queue1&#39;);
$queue->declare();
$queue->bind('exchange1', 'routekey');
Copy after login

Message sent

<?php
$connection = new AMQPConnection();
$connection->connect();
$channel = new AMQPChannel($connection);
$exchange = new AMQPExchange($channel);
$exchange->setName(&#39;exchange5&#39;);
$exchange->setType(&#39;fanout&#39;);
$exchange->declare();
for($i = 0; $i < 2000000; $i++) {
 $exchange->publish("message $i", "routekey");
}
Copy after login

Message received

<?php
$connection = new AMQPConnection();
$connection->connect();
$channel = new AMQPChannel($connection);
$queue = new AMQPQueue($channel);
$queue->setName(&#39;queue1&#39;);
$queue->declare();
$queue->bind('exchange1', 'routekey');
while (true) {
  $queue->consume(function($envelope, $queue){
   echo $envelope->getBody(), PHP_EOL;
  }, AMQP_AUTOACK);
}
Copy after login

Related recommendations:

PHP Message Queuing Service

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!

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