PHP code example of skolodyazhnyy / butter-amqplib

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

    

skolodyazhnyy / butter-amqplib example snippets


use ButterAMQP\ConnectionBuilder;

$connection = ConnectionBuilder::make()
    ->create("//guest:guest@localhost/%2f");

$channel = $connection->channel(1);

use ButterAMQP\ExchangeInterface as Exchange;
use ButterAMQP\QueueInterface as Queue;

$channel->exchange('butter')
    ->define(Exchange::TYPE_FANOUT, Exchange::FLAG_DURABLE);
    
$channel->queue('butter')
    ->define(Queue::FLAG_DURABLE | Queue::FLAG_EXCLUSIVE)
    ->bind('butter');

use ButterAMQP\Message;

// Construct a message to be published
$message = new Message('hi there', ['content-type' => 'text/plain']);

// Publish message to default exchange, with routing key "text-messages".
$channel->publish($message, '', 'text-messages');

use ButterAMQP\Delivery;

// Declare consumer
$consumer = $channel->consume('text-messages', function(Delivery $delivery) {
    echo "Receive a message: " . $delivery->getBody() . PHP_EOL;
    
    // Acknowledge delivery
    $delivery->ack();
});

// Serve connection until consumer is cancelled
while($consumer->isActive()) {
    $connection->serve();
}

$connection->close();