×

phpamqplib使用

phpamqplib使用(php a)

admin admin 发表于2023-03-23 20:54:12 浏览61 评论0

抢沙发发表评论

本文目录一览:

rabbitmq官网中的php测试helloworld的代码是否有问题

?php

require_once __DIR__ . '/vendor/autoload.php';

use PhpAmqpLib\Connection\AMQPConnection;

use PhpAmqpLib\Message\AMQPMessage;

$connection = new AMQPConnection('localhost', 5672, 'guest', 'guest');

$channel = $connection-channel();

$channel-queue_declare('hello', false, false, false, false);

$msg = new AMQPMessage('Hello World!');

$channel-basic_publish($msg, '', 'hello');

echo " [x] Sent 'Hello World!'\n";

$channel-close();

$connection-close();

?

receive.php

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

?php

require_once __DIR__ . '/vendor/autoload.php';

use PhpAmqpLib\Connection\AMQPConnection;

$connection = new AMQPConnection('localhost', 5672, 'guest', 'guest');

$channel = $connection-channel();

$channel-queue_declare('hello', false, false, false, false);

echo ' [*] Waiting for messages. To exit press CTRL+C', "\n";

$callback = function($msg) {

echo " [x] Received ", $msg-body, "\n";

};

$channel-basic_consume('hello', '', false, true, false, false, $callback);

while(count($channel-callbacks)) {

$channel-wait();

}

$channel-close();

$connection-close();

?