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

How to access RabbitMQ message queue using PHP

不言
不言Original
2018-06-07 09:41:572589browse

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(&#39;exchange1&#39;);
$exchange->setType(&#39;fanout&#39;);
$exchange->declare();

Queue establishment

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

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');

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");
}

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);
}

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!

Statement:
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