How to determine the number of messages in the queue from the php script? Here is my code:

<?php require_once __DIR__ . '/vendor/autoload.php'; use PhpAmqpLib\Connection\AMQPStreamConnection; use PhpAmqpLib\Message\AMQPMessage; $connection = new AMQPStreamConnection('localhost', 5672, 'user', 'pass'); $channel = $connection->channel(); $channel->basic_consume('sms_inbox', '', true, false, false, false, $callback); 

    2 answers 2

    This is a crutch to resolve this issue. How to make the output of the number of messages in the queue using php without the exec() function

     $content = exec('rabbitmqctl list_queues|grep sms_inbox'); preg_match_all('/sms_inbox(.+)/', $content, $result, PREG_PATTERN_ORDER); $cnt = trim($result[1][0]); 
    • This is a crutch to resolve this issue. How to make the conclusion of the number of messages in the queue using php without the exec () function; I didn’t work out - vadim
     $queue = new AMQPQueue($channel); $queue->setName('sms_inbox'); $queue->setFlags(AMQP_DURABLE); echo $queue->declare() . PHP_EOL; // до 1.2.0 

    or from rabbitmq version 1.2.0:

     echo $queue->declareQueue() . PHP_EOL; // с 1.2.0 

    The declare() method, in addition to declaring a queue, returns the number of messages in the queue.

    Return values

    Returns the message count.

    Since amqp 1.2.0, AMQPQueue::declare() deprecated and you need to use AMQPQueue::declareQueue() .

    • This will declare a queue, but the answer will definitely not contain anything the number of messages - etki
    • @Etki I have a variant with ->declareQueue() - returns the number of messages. with declare also works, but with an error, because declare deprecated from 1.2.0. - jekaby