### Install Dependencies with Composer Source: https://www.rabbitmq.com/tutorials/tutorial-one-php.html Run this command in your terminal to install the libraries specified in your composer.json file. ```shell php composer.phar install ``` -------------------------------- ### Install php-amqplib via Composer Source: https://www.rabbitmq.com/tutorials/tutorial-one-php.html Use Composer to add the php-amqplib library to your PHP project. Ensure Composer is installed and functional. ```json { "require": { "php-amqplib/php-amqplib": "^3.2" } } ``` -------------------------------- ### Start Message Consumption Source: https://www.rabbitmq.com/tutorials/tutorial-one-php.html Configures the channel to consume messages from the 'hello' queue and starts the consumption loop. This code will block until messages are received or an error occurs. ```php $channel->basic_consume('hello', '', false, true, false, false, $callback); try { $channel->consume(); } catch (\Throwable $exception) { echo $exception->getMessage(); } ``` -------------------------------- ### Establish Connection and Declare Queue Source: https://www.rabbitmq.com/tutorials/tutorial-one-php.html Sets up the connection to the RabbitMQ server and declares the 'hello' queue. Ensure the queue exists before consuming messages. ```php $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest'); $channel = $connection->channel(); $channel->queue_declare('hello', false, true, false, false, false, new AMQPTable(['x-queue-type' => 'quorum'])); echo " [*] Waiting for messages. To exit press CTRL+C\n"; ``` -------------------------------- ### Declare Queue and Publish Message Source: https://www.rabbitmq.com/tutorials/tutorial-one-php.html Declare a queue named 'hello' and publish a 'Hello World!' message to it. Queue declaration is idempotent. ```php $channel->queue_declare('hello', false, true, false, false, false, new AMQPTable(['x-queue-type' => 'quorum'])); $msg = new AMQPMessage('Hello World!'); $channel->basic_publish($msg, '', 'hello'); echo " [x] Sent 'Hello World!'\n"; ``` -------------------------------- ### Include and Use php-amqplib Classes Source: https://www.rabbitmq.com/tutorials/tutorial-one-php.html Include the Composer autoloader and use the necessary classes from the php-amqplib library for your PHP script. ```php require_once __DIR__ . '/vendor/autoload.php'; use PhpAmqpLib\Connection\AMQPStreamConnection; use PhpAmqpLib\Message\AMQPMessage; use PhpAmqpLib\Wire\AMQPTable; ``` -------------------------------- ### List RabbitMQ Queues (Linux/macOS) Source: https://www.rabbitmq.com/tutorials/tutorial-one-php.html Utility command to view all queues in RabbitMQ and their message counts. Requires privileged access. ```bash sudo rabbitmqctl list_queues ``` -------------------------------- ### List RabbitMQ Queues (Windows) Source: https://www.rabbitmq.com/tutorials/tutorial-one-php.html Utility command to view all queues in RabbitMQ and their message counts on a Windows system. ```bash rabbitmqctl.bat list_queues ``` -------------------------------- ### Run Publisher Script Source: https://www.rabbitmq.com/tutorials/tutorial-one-php.html Command to execute the PHP publisher script from the terminal. This sends a message to RabbitMQ for the receiver to process. ```bash php send.php ``` -------------------------------- ### Establish AMQP Stream Connection Source: https://www.rabbitmq.com/tutorials/tutorial-one-php.html Create a connection to the RabbitMQ server using AMQPStreamConnection. This abstracts socket connections and handles protocol negotiation. ```php $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest'); $channel = $connection->channel(); ``` -------------------------------- ### Include and Use Statements for Receiver Source: https://www.rabbitmq.com/tutorials/tutorial-one-php.html These statements are required for the PHP receiver to connect to RabbitMQ and handle messages. ```php require_once __DIR__ . '/vendor/autoload.php'; use PhpAmqpLib\Connection\AMQPStreamConnection; use PhpAmqpLib\Message\AMQPMessage; use PhpAmqpLib\Wire\AMQPTable; ``` -------------------------------- ### Run Receiver Script Source: https://www.rabbitmq.com/tutorials/tutorial-one-php.html Command to execute the PHP receiver script from the terminal. The receiver will continuously listen for messages. ```bash php receive.php ``` -------------------------------- ### Close AMQP Channel and Connection Source: https://www.rabbitmq.com/tutorials/tutorial-one-php.html Properly close the channel and the connection to the RabbitMQ server after completing operations. ```php $channel->close(); $connection->close(); ``` -------------------------------- ### Define Message Consumption Callback Source: https://www.rabbitmq.com/tutorials/tutorial-one-php.html A callback function that processes incoming messages. It echoes the message body to the console. ```php $callback = function (AMQPMessage $msg) { echo ' [x] Received ', $msg->getBody(), "\n"; }; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.