meioa/php-mqlib

A simple library for MQ message listening and sending based on php-amqplib

Installs: 1

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/meioa/php-mqlib

v1.0.0 2025-10-14 03:36 UTC

This package is auto-updated.

Last update: 2025-11-14 03:45:59 UTC


README

A simple library for MQ message listening and sending based on php-amqplib

Installation

composer require meioa/php-mqlib

Example

    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]);