php-amqplib扩展包安装使用

扩展安装

https://www.cnblogs.com/fish-minuet/p/9771096.html

git 地址

https://github.com/php-amqplib/php-amqplib

看云文档地址

https://www.kancloud.cn/lengyueguang/linux/2055371

生产小案例

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

use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
$connection = new AMQPStreamConnection('ip', 5672, 'admin', 'password');
$channel = $connection->channel();
$channel->queue_declare('hello1', false, false, false, false);
$msg = new AMQPMessage('Hello World!');
$channel->basic_publish($msg, '', 'hello1');
$channel->basic_publish($msg, '', 'hello1');
$channel->basic_publish($msg, '', 'hello1');
echo " [x] Sent 'Hello World!'\n";

$channel->close();
$connection->close();

消费 小案例

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

use PhpAmqpLib\Connection\AMQPStreamConnection;


$connection = new AMQPStreamConnection('ip', 5672, 'admin', 'password'
$channel = $connection->channel();
$channel->queue_declare('hello1', 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('hello1', '', false, true, false, false, $callback);
while(count($channel->callbacks)) {
    $channel->wait();
}

$channel->close();
$connection->close();

循环
请先登录后发表评论
  • latest comments
  • 总共0条评论