PHP code example of vladmeh / rabbitmq-client

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

    

vladmeh / rabbitmq-client example snippets


Rabbit::publish('message', '', 'queue-name');

Rabbit::publish('message', 'exchange-name', 'routing-key');

Rabbit::publish('message', 'amq.fanout', '', [
    'hosts' => [
        'vhosts' => 'vhost3'
    ],
    'message' => [
        'content_type' => 'application/json',
    ],
    'exchange_declare' => [
        'type' => 'fanout',
        'auto_delete' => true,
    ]
]);

Rabbit::consume('queue-name', function (AMQPMessage $msg) {
    $msg->ack();
    var_dump($msg->body);
    if ($msg->getMessageCount() === null) {
        $msg->getChannel()->basic_cancel($msg->getConsumerTag());
    }
});

$response = Rabbit::rpc('message', 'queue-name', ['connection' => [
    'read_write_timeout' => 10.0,
    'channel_rpc_timeout' => 10.0
]]);

var_dump($response);
bash
$ php artisan vendor:publish --tag=rabbit