开发者社区 问答 正文

PHP AMQP的问题:报错

装了amqp 1.0.1 PHP的extension...

看着在线文档..写了个程序可以发送消息去消息队列...

但是接收不会写...因为PHP在线关于AMQP的文档范例是错的...

比如AMQPQueue的declare(void)..

但是范例却写declare("xxxx")...

求PHP的amqp 1.0.1 的正常范例....

help me ...thx...

@红薯

展开
收起
kun坤 2020-06-09 23:15:35 670 分享 版权
1 条回答
写回答
取消 提交回答
  • $q->get(AMQP_AUTOACK); 请教关于这个的问题 如果我是这么写 $q->get(); 队列中的数据如何清楚 ######<?php

    //设置你的连接
    $conn_args = array('host' => 'localhost', 'port' => '5672', 'login' => 'guest', 'password' => 'guest');
    $conn = new AMQPConnection($conn_args);
    if ($conn->connect()) {
        echo "Established a connection to the broker \n";
    }
    else {
        echo "Cannot connect to the broker \n ";
    }
    //你的消息
    $message = json_encode(array('Hello World!','php','c++'));
    //创建channel
    $channel = new AMQPChannel($conn);
    //创建exchange
    $ex = new AMQPExchange($channel);
    $ex->setName('exchange');//创建名字
    $ex->setType(AMQP_EX_TYPE_DIRECT);
    $ex->setFlags(AMQP_DURABLE | AMQP_AUTODELETE);
    echo "exchange status:".$ex->declare();
    echo "\n";
    //创建队列
    $q = new AMQPQueue($channel);
    //设置队列名字 如果不存在则添加
    $q->setName('queue');
    $q->setFlags(AMQP_DURABLE | AMQP_AUTODELETE);
    echo "queue status: ".$q->declare();
    echo "\n";
    echo 'queue bind: '.$q->bind('exchange','route.key');//将你的队列绑定到routingKey
    echo "\n";

    $channel->startTransaction();
    echo "send: ".$ex->publish($message, 'route.key'); //将你的消息通过制定routingKey发送
    $channel->commitTransaction();
    $conn->disconnect();

    ?>######

    引用来自“都给”的答案

    <?php

    //设置你的连接
    $conn_args = array('host' => 'localhost', 'port' => '5672', 'login' => 'guest', 'password' => 'guest');
    $conn = new AMQPConnection($conn_args);
    if ($conn->connect()) {
        echo "Established a connection to the broker \n";
    }
    else {
        echo "Cannot connect to the broker \n ";
    }
    //你的消息
    $message = json_encode(array('Hello World!','php','c++'));
    //创建channel
    $channel = new AMQPChannel($conn);
    //创建exchange
    $ex = new AMQPExchange($channel);
    $ex->setName('exchange');//创建名字
    $ex->setType(AMQP_EX_TYPE_DIRECT);
    $ex->setFlags(AMQP_DURABLE | AMQP_AUTODELETE);
    echo "exchange status:".$ex->declare();
    echo "\n";
    //创建队列
    $q = new AMQPQueue($channel);
    //设置队列名字 如果不存在则添加
    $q->setName('queue');
    $q->setFlags(AMQP_DURABLE | AMQP_AUTODELETE);
    echo "queue status: ".$q->declare();
    echo "\n";
    echo 'queue bind: '.$q->bind('exchange','route.key');//将你的队列绑定到routingKey
    echo "\n";

    $channel->startTransaction();
    echo "send: ".$ex->publish($message, 'route.key'); //将你的消息通过制定routingKey发送
    $channel->commitTransaction();
    $conn->disconnect();

    ?>

    我知道publish没问题。。。我要get啊。。。。

    ######<?php
    $conn_args = array('host' => 'localhost', 'port' => '5672', 'login' => 'guest', 'password' => 'guest'
                        ,'vhost'=>'/');
    $conn = new AMQPConnection($conn_args);
    $conn->connect();
    $channel = new AMQPChannel($conn);
    $q = new AMQPQueue($channel);
    $q->setName('queue2');
    $q->setFlags(AMQP_DURABLE | AMQP_AUTODELETE);
    echo "queue status: ".$q->declare();
    echo "==========\n";
        
    $messages = $q->get(AMQP_AUTOACK);
    print_r($messages->getBody());
    echo "\n";
    // disconnect
    $conn->disconnect();

    ?>
    ######
    $conn_args = array('host' => '192.168.4.1', 'port' => '5672', 'login' => 'test', 'password' => 'test' ,'vhost'=>'localhost'); 
    $cnn = new AMQPConnection($conn_args);
    $cnn->connect();
    
    $ch = new AMQPChannel($cnn);
    
    // Declare a new exchange
    $ex = new AMQPExchange($ch);
    $ex->setName('exchange1');
    $ex->setType(AMQP_EX_TYPE_FANOUT);
    $ex->declare();
    
    // Create a new queue
    $q = new AMQPQueue($ch);
    $q->setName('queue1' . time());
    $q->declare();
    
    // Bind it on the exchange to routing.key
    $q->bind($ex->getName(), 'routing.*');
    // Publish a message to the exchange with a routing key
    $ex->publish('message', 'routing.1');
    $ex->publish('message2', 'routing.2');
    $ex->publish('message3', 'routing.3');
    
    
    for ($i = 0; $i < 3; $i++) {
    // Read from the queue
    $msg = $q->get(AMQP_AUTOACK);
        echo $msg->getBody() . "\n";
    }
    
    $ex->delete();

    ######php官方那个确实有问题
    2020-06-09 23:15:42
    赞同 展开评论
问答分类:
问答标签:
问答地址: