PHP code example of troogle / php-amqplib

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

    

troogle / 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
            exit(0);
            break;
        case \SIGHUP:
            // some stuff to restart consumer
            break;
        default:
            // do nothing
    }
};

declare(ticks = 1) {
    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');
javascript
{
  "varo/php-amqplib": "2.5.*"
  }
}
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);