The simplest queue mode of PHP RabbitMQ consists of a producer and a consumer, as shown in the architecture below. PHP RabbitMQ Explanation: P represents the producer, C represents the consumer, and red represents the queue.

Currently, when operating RabbitMQ with PHP, the recommended package by the official is php-amqplib.

1. Pre-tutorial

Please read the following sections first to understand the relevant knowledge:

2. Install PHP dependencies

Use composer to install:

composer require php-amqplib/php-amqplib

Note: The latest version of php-amqplib requires PHP 7.0 or above.

Import the php-amqplib package:

require_once __DIR__ . '/vendor/autoload.php';

3. Send message

3.1. Create RabbitMQ connection

$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');

Parameter explanation:

  • new AMQPStreamConnection('RabbitMQ server address', port number, 'username', 'password');

3.2. Create Channel

$channel = $connection->channel();

Most operations are completed in the channel.

3.3. Declare queue

$channel->queue_declare(
	'tizi365_hello',  // Queue name, must be unique
	false,
	true, // Whether it is durable
	false,
	false
);

3.4. Push message

// Define a message object, the parameter is the message content we need to send
$msg = new AMQPMessage('Hello World!');
// Whether the message is durable
// $msg->set('delivery_mode', AMQPMessage::DELIVERY_MODE_PERSISTENT);

// Send message
$channel->basic_publish(
		$msg, // Message object
		'',   // Ignore the exchange
		'tizi365_hello' // Routing parameter, use the queue name as the routing parameter here
	);

3.5. Complete PHP message sending code

<?php 
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

// Create connection
$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
// Create channel
$channel = $connection->channel();
// Declare queue
$channel->queue_declare('tizi365_hello', false, false, false, false);

// Define message object
$msg = new AMQPMessage('Hello World!');
// Send message
$channel->basic_publish($msg, '', 'tizi365_hello');

echo " [x] Sent 'Hello World!'\n";

$channel->close();
$connection->close();

Save to file: send.php

4. Consuming Messages

The first three steps of message consumption (creating a RabbitMQ connection, creating a channel, declaring a queue) are the same as sending messages, corresponding to sections 3.1, 3.2, and 3.3, respectively.

The complete consumer code is as follows:

<?php require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;

// Create a RabbitMQ connection
$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
// Create a channel
$channel = $connection->channel();

// Declare a queue
$channel->queue_declare('tizi365_hello', false, false, false, false);

echo " [*] Waiting for messages. To exit press CTRL+C\n";

// Define the message handling function (using an anonymous function here)
$callback = function ($msg) {
    // Message handling logic
    echo ' [x] Received ', $msg->body, "\n";
};

// Create a consumer
$channel->basic_consume(
    'tizi365_hello', // Queue name to be consumed
    '', // Consumer tag, if ignored, a unique ID is generated
    false,
    true, // Whether to automatically acknowledge the message, i.e., automatically tell RabbitMQ that the message has been successfully processed
    false,
    false,
    $callback // Message handling function
);

// If the channel is not closed, keep the process blocked to prevent it from exiting
while ($channel->is_open()) {
    $channel->wait();
}

// Release resources
$channel->close();
$connection->close();

Save to file: recv.php

5. Running the Demo

Open two shell windows and execute the two scripts separately:

php recv.php

php send.php