A simple library for MQ message listening and sending based on php-amqplib
composer require meioa/php-mqlib use PhpMqlib\Consumer;
use PhpMqlib\Producer;
//连接配置信息
$config = [
'host' => '127.0.0.1',
'port' => 5672,
'login' => 'guest',
'password' => 'guest',
'vhost' => '/',
];
//定义消息处理函数
$callback = function ($msg){
// 方法2: 使用 has 方法检查后获取
if ($msg->has('content_type')) {
echo " Content-Type (checked): " . $msg->get('content_type') . "\n";
}
var_export($msg->getBody());
echo PHP_EOL;
};
//监听 指定队列,处理消息
(new Consumer($config))->run('q11',$callback);
$data = ['a'=>123,'b'=>'vv'];
//创建 指定交换机 的连接实例
$producer = new Producer($config,'et5');
//发送消息
$producer->send($data,'route');
//发送持久化消息 ,rabbitMq-server 重启后消息不丢失
$producer->setDeliveryMode()->send($data,'route');
//向指定 header 类型交换机 发送 带有headers 参数的 消息
$producer->send($data,'',['h'=>2]);