PHP code example of mouf / oo-amqp-client

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

    

mouf / oo-amqp-client example snippets


use Mouf\AmqpClient\Client;

$client = new Client(
    $rabbitmq_host,
    $rabbitmq_port,
    $rabbitmq_user,
    $rabbitmq_password,
    $rabbitmq_vhost = '/',
    $rabbitmq_insist = false,
    $rabbitmq_login_method = 'AMQPLAIN',
    $rabbitmq_login_response = null,
    $rabbitmq_locale = 'en_US',
    $rabbitmq_connection_timeout = 3.0,
    $rabbitmq_read_write_timeout = 3.0,
    $rabbitmq_context = null,
    $rabbitmq_keepalive = false,
    $rabbitmq_heartbeat = 0
);

public function setPrefetchSize($prefetchSize);
public function setPrefetchCount($prefetchCount);
public function setAGlobal($aGlobal);

use Mouf\AmqpClient\Objects\Exchange;

$exchange = new Exchange($client, 'exchange_name', 'fanout');

public function setPassive($passive);
public function setDurable($durable);
public function setAutoDelete($autoDelete);
public function setInternal($internal);
public function setNowait($nowait);
public function setArguments($arguments);
public function setTicket($ticket);

use Mouf\AmqpClient\Objects\Queue;

$queue = new Queue($client, 'queue_name', [
    new Consumer(function(AMQPMessage $msg) {
        // Do some stuff with the received message
    })
]);

public function setPassive($passive);
public function setDurable($durable);
public function setExclusive($exclusive);
public function setAutoDelete($autoDelete);
public function setNoWait($noWait);
public function setArguments($arguments);
public function setTicket($ticket);
public function setDeadLetterExchange(Exchange $exchange);
public function setConfirm($confirm);
public function setConsumerCancelNotify(Queue $consumerCancelNotify);
public function setAlternateExchange(Queue $alternateExchange);
public function setTtl($ttl);
public function setMaxLength($maxLength);
public function setMaxPriority($maxPriority);

use Mouf\AmqpClient\Objects\Binding;

$binding = new Binding($exchange, $queue);
$client->register($binding);

$exchange->publish(new Message('your message body'), 'message_key');
// ... and that's it!

public function publish(Message $message,
                        string $routingKey,
                        bool $mandatory = false,
                        bool $immediate = false,
                        $ticket = null);

public function setContentType(string $content_type);
public function setContentEncoding(string $content_encoding);
public function setApplicationHeaders(array $application_headers);
public function setDeliveryMode(int $delivery_mode);
public function setPriority(int $priority);
public function setCorrelationId(string $correlation_id);
public function setReplyTo(string $reply_to);
public function setExpiration(string $expiration);
public function setMessageId(string $message_id);
public function setTimestamp(\DateTimeInterface $timestamp);
public function setType(string $type);
public function setUserId(string $user_id);
public function setAppId(string $app_id);
public function setClusterId(string $cluster_id);

$consumerService = new ConsumerService($client, [
    $queue
]);

$consumerService->run();

class MyConsumer extends AbstractConsumer
{
    public function onMessageReceived($msg)
    {
        // Do some stuff.
    }
}

use Mouf\AmqpClient\Objects\DefaultExchange;

$exchange = new DefaultExchange($client);
// Simply pass the queue name as the second parameter of "publish".
// Note: you do not need to bind the queue to the exchange. RabbitMQ does this automatically.
$exchange->publish(new Message('your message body'), 'name_of_the_target_queue');
// ... and that's it!

use Mouf\AmqpClient\Objects\Queue;

$queue = new Queue($client, 'queue_name', [
    new Consumer(function(AMQPMessage $msg) {
        // Do some stuff with the received message
    })
]);

// Shazam! We are directly sending a message to the queue. No exchange needed!
$queue->publish(new Message('your message body'));