PHP code example of mmanazar / laravel-amqp

1. Go to this page and download the library: Download mmanazar/laravel-amqp 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/ */

    

mmanazar / laravel-amqp example snippets


return [

    'use' => 'production',

    'properties' => [

        'production' => [
            'host'                => '',
            'port'                => '',
            'username'            => '',
            'password'            => '',
            'vhost'               => '/',
            'connect_options'     => [],
            'ssl_options'         => [],

            'exchange'              => 'amq.topic',
            'exchange_type'         => 'topic',
            'exchange_passive'      => false,
            'exchange_durable'      => true,
            'exchange_auto_delete'  => false,
            'exchange_internal'     => false,
            'exchange_nowait'       => false,
            'exchange_properties'   => [],

            'queue_force_declare'   => false,
            'queue_passive'         => false,
            'queue_durable'         => true,
            'queue_exclusive'       => false,
            'queue_auto_delete'     => false,
            'queue_nowait'          => false,
            'queue_properties'      => ['x-ha-policy' => ['S', 'all']],

            'consumer_tag'      => '',
            'consumer_no_local'   => false,
            'consumer_no_ack'     => true,
            'consumer_exclusive'  => false,
            'consumer_nowait'     => false,
            'timeout'             => 0,
            'persistent'          => true,
        ],

    ],

];

/*
|--------------------------------------------------------------------------
| Register Service Providers
|--------------------------------------------------------------------------
*/

//...

$app->configure('amqp');
$app->register(Bschmitt\Amqp\LumenServiceProvider::class);

//...

//...
$app->withFacades();
class_alias(\Illuminate\Support\Facades\App::class, 'App');
//...

'Bschmitt\Amqp\AmqpServiceProvider',

'Amqp' => 'Bschmitt\Amqp\Facades\Amqp',

    Amqp::publish('routing-key', 'message');
	
    Amqp::publish('routing-key', 'message' , ['queue' => 'queue-name']);
	
    Amqp::publish('routing-key', 'message' , ['exchange' => 'amq.direct']);

    Amqp::publish('routing-key', ['message1', 'message2', ......], a);

Amqp::consume('queue-name', function ($message, $resolver) {
    		
   var_dump($message->body);

   $resolver->acknowledge($message);

   $resolver->stopWhenProcessed();
        
});

Amqp::consume('queue-name', function ($message, $resolver) {
    		
   var_dump($message->body);

   $resolver->acknowledge($message);
        
});

Amqp::consume('queue-name', function ($message, $resolver) {
    		
   var_dump($message->body);

   $resolver->acknowledge($message);
      
}, [
	'timeout' => 2,
	'vhost'   => 'vhost3'
]);

\Amqp::publish('', 'message' , [
    'exchange_type' => 'fanout',
    'exchange' => 'amq.fanout',
]);

\Amqp::consume('', function ($message, $resolver) {
    var_dump($message->body);
    $resolver->acknowledge($message);
}, [
    'exchange' => 'amq.fanout',
    'exchange_type' => 'fanout',
    'queue_force_declare' => true,
    'queue_exclusive' => true,
    'persistent' => true// 
batch
$ php composer update

$ php composer