PHP code example of swoft / php-amqplib

1. Go to this page and download the library: Download swoft/php-amqplib 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/ */

    

swoft / php-amqplib example snippets




use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

$msg = new AMQPMessage($msg_body);
$ch->batch_basic_publish($msg, $exchange);

$msg2 = new AMQPMessage($msg_body);
$ch->batch_basic_publish($msg2, $exchange);

$ch->publish_batch();

$msg->setBody($body2);
$ch->basic_publish($msg, $exchange);

$pcntlHandler = function ($signal) {
    switch ($signal) {
        case \SIGTERM:
        case \SIGUSR1:
        case \SIGINT:
            // some stuff before stop consumer e.g. delete lock etc
            pcntl_signal($signal, SIG_DFL); // restore handler
            posix_kill(posix_getpid(), $signal); // kill self with signal, see https://www.cons.org/cracauer/sigint.html
        case \SIGHUP:
            // some stuff to restart consumer
            break;
        default:
            // do nothing
    }
};

pcntl_signal(\SIGTERM, $pcntlHandler);
pcntl_signal(\SIGINT,  $pcntlHandler);
pcntl_signal(\SIGUSR1, $pcntlHandler);
pcntl_signal(\SIGHUP,  $pcntlHandler);


define('AMQP_WITHOUT_SIGNALS', true);

... more code



define('AMQP_DEBUG', true);

... more code


define('AMQP_PROTOCOL', '0.8');
bash
$ composer 
bash
$ cd php-amqplib/demo
$ php amqp_consumer.php
bash
$ cd php-amqplib/demo
$ php amqp_publisher.php some text to publish
bash
$ php amqp_publisher.php quit
bash
$ php amqp_consumer_non_blocking.php

$properties = array('content_type' => 'text/plain', 'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT);
$msg = new AMQPMessage($body, $properties);
$ch->basic_publish($msg, $exchange);