andrewmy/rabbitmq-management-api
PHP 7.1+/8 wrapper for the RabbitMQ Management HTTP API. Provides an object-oriented client for queues, exchanges, publish, and more, using PHP-HTTP/HTTPlug so you can plug in any compatible HTTP client (e.g., Guzzle adapter).
Installation:
composer require andrewmy/rabbitmq-management-api php-http/guzzle7-adapter
First Use Case:
$client = new \RabbitMq\ManagementApi\Client(
'http://guest:guest@localhost:15672/api',
new \Http\Adapter\Guzzle\Guzzle17Adapter()
);
$vhosts = $client->vhosts()->get('/');
Key Entry Points:
$client->queues()$client->exchanges()$client->bindings()$client->channels()Queue Management:
$client->queues()->declareQueue('/', 'my-queue', [
'auto_delete' => false,
'durable' => true,
]);
$response = $client->exchanges()->publish('/', 'my-exchange', [
'routing_key' => 'my-routing-key',
'payload' => json_encode(['data' => 'test']),
'payload_encoding' => 'application/json',
]);
Exchange Operations:
$exchanges = $client->exchanges()->get('/');
$client->exchanges()->delete('/', 'unwanted-exchange');
Consumer Monitoring:
$consumers = $client->consumers()->get('/', 'my-queue');
$client->consumers()->cancel('/', 'my-queue', $consumerTag);
Error Handling:
Use try/catch with \RabbitMq\ManagementApi\Exception\ApiException for HTTP errors.
try {
$client->queues()->purge('/', 'my-queue');
} catch (\RabbitMq\ManagementApi\Exception\ApiException $e) {
log::error('RabbitMQ API Error: ' . $e->getMessage());
}
Bulk Operations:
Leverage getAll() methods for large datasets (e.g., $client->queues()->getAll('/')).
Authentication: Use environment variables for credentials:
$client = new \RabbitMq\ManagementApi\Client(
sprintf('http://%s:%s@%s:%d/api',
env('RABBITMQ_USER'),
env('RABBITMQ_PASS'),
env('RABBITMQ_HOST'),
env('RABBITMQ_PORT')
)
);
Async Operations: Combine with Laravel Queues for delayed tasks:
Queue::push(function () use ($client) {
$client->queues()->purge('/', 'temp-queue');
});
HTTP Client Compatibility:
guzzle7-adapter vs. symfony-http-client).Rate Limiting:
use Symfony\Component\HttpClient\RetryableHttpClient;
$client = new RetryableHttpClient(
new \Http\Adapter\Guzzle\Guzzle17Adapter(),
[
'max_retries' => 3,
'delay' => 100,
'multiplier' => 2,
]
);
Vhost Isolation:
/ (or correct vhost) in API paths. Omitting it defaults to /.Payload Encoding:
payload_encoding is 'string'. Use 'application/json' for structured data and handle serialization manually:
$payload = json_encode(['key' => 'value'], JSON_THROW_ON_ERROR);
Enable HTTP Logging: Configure Guzzle to log requests/responses:
$client = new \Http\Adapter\Guzzle\Guzzle17Adapter(
new \GuzzleHttp\Client([
'handler' => \GuzzleHttp\HandlerStack::create(
new \GuzzleHttp\Middleware::tap(function ($request, $options) {
\Log::debug('Request:', [
'url' => (string) $request->getUri(),
'method' => $request->getMethod(),
]);
})
),
])
);
API Response Inspection:
The package returns raw API responses. Use json_decode($response, true) to inspect structure:
$response = $client->queues()->get('/', 'my-queue');
\Log::debug('Queue Details:', $response);
Custom HTTP Clients:
Extend the Client class to add middleware or logging:
class CustomClient extends \RabbitMq\ManagementApi\Client {
public function __construct() {
parent::__construct(
'http://...',
new \Http\Adapter\Guzzle\Guzzle17Adapter(),
new \Http\Message\Formatter\SocketFactory()
);
$this->getHttpClient()->addMiddleware(...);
}
}
Model Layer: Create domain-specific models to wrap API responses:
class QueueModel {
public function __construct(private array $data) {}
public function getMessageCount(): int {
return $this->data['messages'] ?? 0;
}
}
Event Listeners: Use Laravel’s event system to react to queue/exchange changes:
$client->queues()->declareQueue('/', 'event-queue', [], function ($response) {
event(new QueueDeclared($response['name']));
});
Default Vhost:
The package defaults to / (root vhost). Explicitly pass the vhost in all methods to avoid ambiguity.
Type Safety: The package uses loose typing for payloads. Validate inputs before publishing:
$payload = is_string($payload) ? $payload : json_encode($payload);
How can I help you explore Laravel packages today?