PHPackages                             troogle/php-amqplib - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Queues &amp; Workers](/categories/queues)
4. /
5. troogle/php-amqplib

ActiveLibrary[Queues &amp; Workers](/categories/queues)

troogle/php-amqplib
===================

This library is a pure PHP implementation of the AMQP protocol. It's been tested against RabbitMQ.

v2.6.2(10y ago)040LGPL-2.1PHPPHP &gt;=5.3.0

Since Mar 28Pushed 10y ago9 watchersCompare

[ Source](https://github.com/tripda/php-amqplib)[ Packagist](https://packagist.org/packages/troogle/php-amqplib)[ Docs](https://github.com/videlalvaro/php-amqplib/)[ RSS](/packages/troogle-php-amqplib/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (2)Versions (29)Used By (0)

php-amqplib
===========

[](#php-amqplib)

[![Latest Version on Packagist](https://camo.githubusercontent.com/2532cfd02f177ecfa14d72201467b6bf49c592ca22ba3f5c5cf682393d6e7002/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f766964656c616c7661726f2f7068702d616d71706c69622e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/videlalvaro/php-amqplib)[![Software License](https://camo.githubusercontent.com/2bc95db9d4d6b319fe40fe1a46431a18f9684b30d516775115c5d0df6aa3e9b4/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4c47504c2d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![Build Status](https://camo.githubusercontent.com/8e276c4fb6e7f0b67a25032c2d837684c6e6adf6fda5011fbe331f7f9f2e5aaf/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f766964656c616c7661726f2f7068702d616d71706c69622f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/videlalvaro/php-amqplib)[![Coverage Status](https://camo.githubusercontent.com/269bf3928da5af1a7f307d6fc6eca5a0a9a83ca8c5f0ee1053430840796df291/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f766964656c616c7661726f2f7068702d616d71706c69622e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/videlalvaro/php-amqplib/code-structure)[![Quality Score](https://camo.githubusercontent.com/d3c6859bc5818bd7725375ed2c96a8fda5fbe2435f0d00b35005be5b78688549/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f766964656c616c7661726f2f7068702d616d71706c69622e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/videlalvaro/php-amqplib)[![Total Downloads](https://camo.githubusercontent.com/7521b89c649ab32e727b6625eeff07c9d9044870312b9e3ef2adafd72ea5878a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f766964656c616c7661726f2f7068702d616d71706c69622e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/videlalvaro/php-amqplib)

**This fork has the Heartbeat feature and its life is until the official repository approves the Heartbeat's pull request**

This library is a *pure PHP* implementation of the AMQP protocol. It's been tested against [RabbitMQ](http://www.rabbitmq.com/).

**Requirements: PHP 5.3** due to the use of `namespaces`.

The library was used for the PHP examples of [RabbitMQ in Action](http://manning.com/videla/) and the [official RabbitMQ tutorials](http://www.rabbitmq.com/tutorials/tutorial-one-php.html).

Supported RabbitMQ Versions
---------------------------

[](#supported-rabbitmq-versions)

Starting with version 2.0 this library uses `AMQP 0.9.1` by default and thus requires [RabbitMQ 2.0 or later version](http://www.rabbitmq.com/download.html). You shouldn't need to change your code, but test before upgrading.

Supported RabbitMQ Extensions
-----------------------------

[](#supported-rabbitmq-extensions)

Since the library uses `AMQP 0.9.1` we added support for the following RabbitMQ extensions:

- Exchange to Exchange Bindings
- Basic Nack
- Publisher Confirms
- Consumer Cancel Notify

Extensions that modify existing methods like `alternate exchanges` are also supported.

Setup
-----

[](#setup)

Add a `composer.json` file to your project:

```
{
  "require": {
      "videlalvaro/php-amqplib": "2.5.*"
  }
}
```

Then provided you have [composer](http://getcomposer.org) installed, you can run the following command:

```
$ composer.phar install
```

That will fetch the library and its dependencies inside your vendor folder. Then you can add the following to your .php files in order to use the library

```
require_once __DIR__.'/vendor/autoload.php';
```

Then you need to `use` the relevant classes, for example:

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

Usage
-----

[](#usage)

With RabbitMQ running open two Terminals and on the first one execute the following commands to start the consumer:

```
$ cd php-amqplib/demo
$ php amqp_consumer.php
```

Then on the other Terminal do:

```
$ cd php-amqplib/demo
$ php amqp_publisher.php some text to publish
```

You should see the message arriving to the process on the other Terminal

Then to stop the consumer, send to it the `quit` message:

```
$ php amqp_publisher.php quit
```

If you need to listen to the sockets used to connect to RabbitMQ then see the example in the non blocking consumer.

```
$ php amqp_consumer_non_blocking.php
```

Change log
----------

[](#change-log)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Tutorials
---------

[](#tutorials)

To not repeat ourselves, if you want to learn more about this library, please refer to the [official RabbitMQ tutorials](http://www.rabbitmq.com/tutorials/tutorial-one-php.html).

More Examples
-------------

[](#more-examples)

- `amqp_ha_consumer.php`: demos the use of mirrored queues
- `amqp_consumer_exclusive.php` and `amqp_publisher_exclusive.php`: demos fanout exchanges using exclusive queues.
- `amqp_consumer_fanout_{1,2}.php` and `amqp_publisher_fanout.php`: demos fanout exchanges with named queues.
- `basic_get.php`: demos obtaining messages from the queues by using the *basic get* AMQP call.

Batch Publishing
----------------

[](#batch-publishing)

Let's say you have a process that generates a bunch of messages that are going to be published to the same `exchange` using the same `routing_key` and options like `mandatory`. Then you could make use of the `batch_basic_publish` library feature. You can batch messages like this:

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

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

and then send the batch like this:

```
$ch->publish_batch();
```

### When do we publish the message batch?

[](#when-do-we-publish-the-message-batch)

Let's say our program needs to read from a file and then publish one message per line. Depending on the message size, you will have to decide when it's better to send the batch. You could send it every 50 messages, or every hundred. That's up to you.

Optimized Message Publishing
----------------------------

[](#optimized-message-publishing)

Another way to speed up your message publishing is by reusing the `AMQPMessage` message instances. You can create your new message like this:

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

```

Now let's say that while you want to change the message body for future messages, you will keep the same properties, that is, your messages will still be `text/plain` and the `delivery_mode` will still be `AMQPMessage::DELIVERY_MODE_PERSISTENT`. If you create a new `AMQPMessage` instance for every published message, then those properties would have to be re-encoded in the AMQP binary format. You could avoid all that by just reusing the `AMQPMessage` and then resetting the message body like this:

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

Truncating Large Messages
-------------------------

[](#truncating-large-messages)

AMQP imposes no limit on the size of messages; if a very large message is received by a consumer, PHP's memory limit may be reached within the library before the callback passed to `basic_consume` is called.

To avoid this, you can call the method `AMQPChannel::setBodySizeLimit(int $bytes)` on your Channel instance. Body sizes exceeding this limit will be truncated, and delivered to your callback with a `AMQPMessage::$is_truncated` flag set to `true`. The property `AMQPMessage::$body_size` will reflect the true body size of a received message, which will be higher than `strlen(AMQPMessage::getBody())` if the message has been truncated.

Note that all data above the limit is read from the AMQP Channel and immediately discarded, so there is no way to retrieve it within your callback. If you have another consumer which can handle messages with larger payloads, you can use `basic_reject` or `basic_nack` to tell the server (which still has a complete copy) to forward it to a Dead Letter Exchange.

By default, no truncation will occur. To disable truncation on a Channel that has had it enabled, pass `0` (or `null`) to `AMQPChannel::setBodySizeLimit()`.

\##UNIX Signals##

If you have installed [PCNTL extension](http://www.php.net/manual/en/book.pcntl.php) dispatching of signal will be handled when consumer is not processing message.

```
$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);
}
```

To disable this feature just define constant `AMQP_WITHOUT_SIGNALS` as `true`

```
