Assuming that you have a basic understanding of RabbitMQ and is already installed. You would ideally use AMQP library to communicate with the broker, but I encountered difficulties making it work with SC, although I didn’t invest much effort in attempting to do so. Given that my specific requirement was solely to send messages from SC, I opted to utilize the RabbitMQ built-in API for message publication. For the consumers, I rely on plain PHP with AMQP, without incorporating SC or other frameworks.
API Example
<?php
$queueName = ‘your_queue_name’;
$baseUrl = ‘http://localhost:15672/api’; // RabbitMQ management API URL
$username = ‘your_username’;
$password = ‘your_password’;
// Message to send
$message = ‘Hello World’;
// Create a cURL handle
$ch = curl_init();
// Set cURL options
curl_setopt($ch, CURLOPT_URL, $baseUrl . ‘/queues/%2F/’ . $queueName . ‘/publish’);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, ‘POST’);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([‘properties’ => [], ‘routing_key’ => $queueName, ‘payload’ => $message, ‘payload_encoding’ => ‘string’]));
curl_setopt($ch, CURLOPT_USERPWD, $username . ‘:’ . $password);
curl_setopt($ch, CURLOPT_HTTPHEADER, [‘Content-Type: application/json’]);
// Execute cURL request
$response = curl_exec($ch);
// Check for errors
if (curl_errno($ch)) {
echo 'Error: ’ . curl_error($ch);
} else {
echo 'Message sent to the queue: ’ . $message;
}
// Close cURL handle
curl_close($ch);