PHP code example of meioa / php-mqlib

1. Go to this page and download the library: Download meioa/php-mqlib library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

meioa / php-mqlib example snippets


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

bash
composer