php从rabbitmq获取消息错误
我的amqp扩展版本是1.0.1,amqp协议版本是0-9-1 从队列中获取消息:php从rabbitmq获取消息错误,php,rabbitmq,Php,Rabbitmq,我的amqp扩展版本是1.0.1,amqp协议版本是0-9-1 从队列中获取消息: <?php try { $conn = new AMQPConnection() ; $conn->setLogin('guest') ; $conn->setPassword('guest') ; $conn->connect() ; if ($conn->isConnected()) { $channel = new AMQPChannel($conn) ; if
<?php
try {
$conn = new AMQPConnection() ;
$conn->setLogin('guest') ;
$conn->setPassword('guest') ;
$conn->connect() ;
if ($conn->isConnected()) {
$channel = new AMQPChannel($conn) ;
if ($channel->isConnected())
{
$queue = new AMQPQueue($channel) ;
$queue->setName('test_queue') ;
$queue->setFlags(AMQP_DURABLE | AMQP_AUTODELETE) ;
$queue->declare() ;
$messages = $queue->get(AMQP_AUTOACK) ;
print_r($messages->getBody()) ;
}
} else {
echo "connect failure ... " ;
}
$conn->disconnect() ;} catch (Exception $e) {
echo $e->getMessage() ;}?>
在我看来,队列已经存在,并且它以前是在vhost中使用不同的参数声明(创建)的。每次都需要使用相同的参数声明队列(或者使用所需的参数删除和重新创建队列)。尝试通过管理插件删除队列(http://www.rabbitmq.com/management.html)然后再次运行脚本如果队列已经创建,则无需创建它(使用“declare”方法)并再次与exchange绑定。我知道你不应该这样做,因为a)这些操作需要管理权限b)只执行一次就足够了c)你可能没有生产的管理权限,你的代码可能会被破坏。 我认为最好使用管理控制台或您喜欢的任何其他工具创建并绑定所有必需的队列,然后以这种方式接收消息
// consider using connection more than once. that's only for illustration purposes.
$connection = new AMQPConnection([ put your credentials here ]);
$connection->connect();
if(!$connection->isConnected()) {
throw new Exception('Connection failed.');
}
$chnlObj = new AMQPChannel($connection);
$queObj = new AMQPQueue($chnlObj);
$queObj->setName('yourQueueName');
echo $queObj->get(AMQP_AUTOACK)->getBody();
// consider using connection more than once. that's only for illustration purposes.
$connection->disconnect();
// consider using connection more than once. that's only for illustration purposes.
$connection = new AMQPConnection([ put your credentials here ]);
$connection->connect();
if(!$connection->isConnected()) {
throw new Exception('Connection failed.');
}
$chnlObj = new AMQPChannel($connection);
$queObj = new AMQPQueue($chnlObj);
$queObj->setName('yourQueueName');
echo $queObj->get(AMQP_AUTOACK)->getBody();
// consider using connection more than once. that's only for illustration purposes.
$connection->disconnect();