1. Go to this page and download the library: Download byjg/rabbitmq-client library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
byjg / rabbitmq-client example snippets
// Register the connector and associate with a scheme
ConnectorFactory::registerConnector(RabbitMQConnector::class);
// Create a connector
$connector = ConnectorFactory::create(new Uri("amqp://$user:$pass@$host:$port/$vhost"));
// Create a queue
$pipe = new Pipe("test");
$pipe->withDeadLetter(new Pipe("dlq_test"));
// Create a message
$message = new Message("Hello World");
// Publish the message into the queue
$connector->publish(new Envelope($pipe, $message));
// Register the connector and associate with a scheme
ConnectorFactory::registerConnector(RabbitMQConnector::class);
// Create a connector
$connector = ConnectorFactory::create(new Uri("amqp://$user:$pass@$host:$port/$vhost"));
// Create a queue
$pipe = new Pipe("test");
$pipe->withDeadLetter(new Pipe("dlq_test"));
// Connect to the queue and wait to consume the message
$connector->consume(
$pipe, // Queue name
function (Envelope $envelope) { // Callback function to process the message
echo "Process the message";
echo $envelope->getMessage()->getBody();
return Message::ACK;
},
function (Envelope $envelope, $ex) { // Callback function to process the failed message
echo "Process the failed message";
echo $ex->getMessage();
return Message::REQUEUE;
}
);
$pipe = new Pipe("priority_queue");
$pipe->withProperty('x-max-priority', 10); // Set max priority to 10
$highPriorityMessage = new Message("High Priority Message");
$highPriorityMessage->withProperty('priority', 10); // Highest priority
$mediumPriorityMessage = new Message("Medium Priority Message");
$mediumPriorityMessage->withProperty('priority', 5); // Medium priority
$lowPriorityMessage = new Message("Low Priority Message");
$lowPriorityMessage->withProperty('priority', 1); // Low priority
// Messages will be consumed in priority order (highest first)
$connector->publish(new Envelope($pipe, $lowPriorityMessage));
$connector->publish(new Envelope($pipe, $highPriorityMessage));
$connector->publish(new Envelope($pipe, $mediumPriorityMessage));
// Configure the connection with robust settings
$connectionUri = "amqp://$user:$pass@$host:$port/$vhost?heartbeat=30&connection_timeout=10&max_attempts=5&timeout=60";
$connector = ConnectorFactory::create(new Uri($connectionUri));
// Test the connection before proceeding
$rabbitConnector = new RabbitMQConnector();
$rabbitConnector->setUp(new Uri($connectionUri));
if (!$rabbitConnector->testConnection()) {
die("Failed to connect to RabbitMQ server.\n");
}
text
PHP Fatal error: Uncaught PhpAmqpLib\Exception\AMQPProtocolChannelException: PRECONDITION_FAILED - Existing queue 'test' declared with other arguments in AMQPChannel.php:224
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.