Location: PHPKode > scripts > php-amqplib > videlalvaro-php-amqplib-b0b8696/demo/amqp_consumer.php

require_once(__DIR__ . '/../amqp.inc');
include(__DIR__ . '/config.php');

$exchange = 'router';
$queue = 'msgs';
$consumer_tag = 'consumer';

$conn = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);
$ch = $conn->channel();

    The following code is the same both in the consumer and the producer.
    In this way we are sure we always have a queue to consume from and an
        exchange where to publish messages.

    name: $queue
    passive: false
    durable: true // the queue will survive server restarts
    exclusive: false // the queue can be accessed in other channels
    auto_delete: false //the queue won't be deleted once the channel is closed.
$ch->queue_declare($queue, false, true, false, false);

    name: $exchange
    type: direct
    passive: false
    durable: true // the exchange will survive server restarts
    auto_delete: false //the exchange won't be deleted once the channel is closed.

$ch->exchange_declare($exchange, 'direct', false, true, false);

$ch->queue_bind($queue, $exchange);

function process_message($msg) {

    echo "\n--------\n";
    echo $msg->body;
    echo "\n--------\n";


    // Send a message with the string "quit" to cancel the consumer.
    if ($msg->body === 'quit') {

    queue: Queue from where to get the messages
    consumer_tag: Consumer identifier
    no_local: Don't receive messages published by this consumer.
    no_ack: Tells the server if the consumer will acknowledge the messages.
    exclusive: Request exclusive consumer access, meaning only this consumer can access the queue
    callback: A PHP Callback

$ch->basic_consume($queue, $consumer_tag, false, false, false, false, 'process_message');

function shutdown($ch, $conn){
register_shutdown_function('shutdown', $ch, $conn);

// Loop as long as the channel has callbacks registered
while(count($ch->callbacks)) {
Return current item: php-amqplib