PHP code example of dimkamonster / laminas-rabbitmq

1. Go to this page and download the library: Download dimkamonster/laminas-rabbitmq 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/ */

    

dimkamonster / laminas-rabbitmq example snippets




return [
    'rabbitmq' => [
        'host'     => 'localhost',
        'port'     => 5672,
        'login'    => 'guest',
        'password' => 'guest',
    ],
]



/**
 * @var \RabbitMQ\Service\RabbitMQ $rabbitmqService
 * @var \Interop\Container\ContainerInterface $container
 */

$rabbitMQ = $container->get(\RabbitMQ\Service\RabbitMQ::class);
$publisher = new \RabbitMQ\Publisher\WorkQueuePublisher('test_work_queue');
$job = new \RabbitMQ\Job\Job(['some' => 'data']);

$rabbitMQ->setPublisher($publisher);
$rabbitMQ->push($job);



/**
 * @var \RabbitMQ\Service\RabbitMQ $rabbitmqService
 * @var \Interop\Container\ContainerInterface $container
 */

$rabbitMQ = $container->get(\RabbitMQ\Service\RabbitMQ::class);
$consumer = new \RabbitMQ\Consumer\WorkQueueConsumer('test_work_queue');
$rabbitMQ->setConsumer($consumer);

$rabbitMQ->receive(function (\RabbitMQ\Consumer\Message $message) {
    echo $message->getBody();

    $message->ack();
});