PHP code example of yangweijie / es-bunny

1. Go to this page and download the library: Download yangweijie/es-bunny 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/ */

    

yangweijie / es-bunny example snippets


use Yangweijie\EsBunny\SynchronousClient;

$client = new SynchronousClient([
    'host' => '127.0.0.1',
    'port' => 5672,
    'vhost' => '/',
    'user' => 'guest',
    'password' => 'guest',
]);

try {
    $client->connect();
    $channel = $client->channel();
    $channel->queueDeclare('test-queue', false, true, false, false);
    
    // 发布消息
    $channel->publish('Hello World!', [], '', 'test-queue');
    
    // 消费消息
    $message = $channel->get('test-queue');
    if ($message) {
        echo "Received: " . $message->content . "\n";
        $channel->ack($message);
    }
    
    $client->disconnect();
} catch (\Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
}

use Yangweijie\EsBunny\EasySwooleClient;

$client = new EasySwooleClient([
    'host' => '127.0.0.1',
    'port' => 5672,
    'vhost' => '/',
    'user' => 'guest',
    'password' => 'guest',
]);

try {
    $client->connect();
    $channel = $client->channel();
    $channel->queueDeclare('test-queue', false, true, false, false);
    
    // 发布消息
    $channel->publish('Hello World!', [], '', 'test-queue');
    
    // 消费消息
    $message = $channel->get('test-queue');
    if ($message) {
        echo "Received: " . $message->content . "\n";
        $channel->ack($message);
    }
    
    $client->disconnect();
} catch (\Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
}
bash
php example/basic.php