2017 © Pedro Peláez
 

library php-amqplib

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

image

sergiuszm/php-amqplib

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

  • Wednesday, September 10, 2014
  • by sergiuszm
  • Repository
  • 0 Watchers
  • 0 Stars
  • 13 Installations
  • 1 Dependents
  • 0 Suggesters
  • 660 Forks
  • 0 Open issues
  • 25 Versions
  • 0 % Grown

The README.md

php-amqplib

Build Status, (*1)

This library is a pure PHP implementation of the AMQP protocol. It's been tested against RabbitMQ., (*2)

Requirements: PHP 5.3 due to the use of namespaces., (*3)

The library was used for the PHP examples of RabbitMQ in Action and the official RabbitMQ tutorials., (*4)

BC BREAKING CHANGES

Since version 2.0 this library uses AMQP 0.9.1 by default. You shouldn't need to change your code, but test before upgrading., (*5)

Supported RabbitMQ Extensions

Since the library uses AMQP 0.9.1 we added support for the following RabbitMQ extensions:, (*6)

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

Extensions that modify existing methods like alternate exchanges are also supported., (*7)

Setup

Add a composer.json file to your project:, (*8)

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

Then provided you have composer installed, you can run the following command:, (*9)

$ 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, (*10)

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

Then you need to use the relevant classes, for example:, (*11)

use PhpAmqpLib\Connection\AMQPConnection;
use PhpAmqpLib\Message\AMQPMessage;

Usage

With RabbitMQ running open two Terminals and on the first one execute the following commands to start the consumer:, (*12)

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

Then on the other Terminal do:, (*13)

$ 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, (*14)

Then to stop the consumer, send to it the quit message:, (*15)

$ 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., (*16)

$ php amqp_consumer_non_blocking.php

Tutorials

To not repeat ourselves, if you want to learn more about this library, please refer to the official RabbitMQ tutorials., (*17)

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

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:, (*18)

$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:, (*19)

$ch->publish_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., (*20)

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:, (*21)

$properties = array('content_type' => 'text/plain', 'delivery_mode' => 2);
$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 2. 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:, (*22)

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

UNIX Signals

If you have installed PCNTL extension dispatching of signal will be handled when consumer is not processing message., (*23)

$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, (*24)


Benchmarks

To run the publishing/consume benchmark type:, (*25)

$ make benchmark

Tests

To successfully run the tests you need to first setup the test user and test virtual host., (*26)

You can do that by running the following commands after starting RabbitMQ:, (*27)

$ rabbitmqctl add_vhost phpamqplib_testbed
$ rabbitmqctl add_user phpamqplib phpamqplib_password
$ rabbitmqctl set_permissions -p phpamqplib_testbed phpamqplib ".*" ".*" ".*"

Once your environment is set up you can run your tests like this:, (*28)

$ make test

Using AMQP 0.8

If you still want to use the old version of the protocol then you can do it by settings the following constant in your configuration code:, (*29)

define('AMQP_PROTOCOL', '0.8');

The default value is '0.9.1'., (*30)

Providing your own autoloader

If for some reasone you don't want to use composer, then you need to have an autoloader in place fo the library classes. People have reported to use this autoloader with success., (*31)

Original README:

Below is the original README file content. Credits goes to the original authors., (*32)

PHP library implementing Advanced Message Queuing Protocol (AMQP)., (*33)

The library is port of python code of py-amqplib http://barryp.org/software/py-amqplib/, (*34)

It have been tested with RabbitMQ server., (*35)

Project home page: http://code.google.com/p/php-amqplib/, (*36)

For discussion, please join the group:, (*37)

http://groups.google.com/group/php-amqplib-devel, (*38)

For bug reports, please use bug tracking system at the project page., (*39)

Patches are very welcome!, (*40)

Author: Vadim Zaliva lord@crocodile.org, (*41)

The Versions

10/09 2014

dev-master

9999999-dev https://github.com/videlalvaro/php-amqplib/

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

  Sources   Download

LGPL-2.1

The Requires

  • php >=5.3.0
  • ext-bcmath *

 

The Development Requires

rabbitmq queue message

10/09 2014

dev-upstream-master

dev-upstream-master https://github.com/videlalvaro/php-amqplib/

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

  Sources   Download

LGPL-2.1

The Requires

  • php >=5.3.0
  • ext-bcmath *

 

The Development Requires

rabbitmq queue message

10/09 2014

v2.4.3

2.4.3.0 https://github.com/videlalvaro/php-amqplib/

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

  Sources   Download

LGPL-2.1

The Requires

  • php >=5.3.0
  • ext-bcmath *

 

The Development Requires

rabbitmq queue message

10/09 2014

dev-develop

dev-develop https://github.com/videlalvaro/php-amqplib/

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

  Sources   Download

LGPL-2.1

The Requires

  • php >=5.3.0
  • ext-bcmath *

 

The Development Requires

rabbitmq queue message

10/09 2014

v2.4.2

2.4.2.0 https://github.com/videlalvaro/php-amqplib/

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

  Sources   Download

LGPL-2.1

The Requires

  • php >=5.3.0
  • ext-bcmath *

 

The Development Requires

rabbitmq queue message

10/09 2014

v2.4.1

2.4.1.0 https://github.com/videlalvaro/php-amqplib/

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

  Sources   Download

LGPL-2.1

The Requires

  • php >=5.3.0
  • ext-bcmath *

 

The Development Requires

rabbitmq queue message

21/06 2014

v2.4.0

2.4.0.0 https://github.com/videlalvaro/php-amqplib/

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

  Sources   Download

LGPL-2.1

The Requires

  • php >=5.3.0
  • ext-bcmath *

 

The Development Requires

rabbitmq queue message

06/05 2014

v2.3.0

2.3.0.0 https://github.com/videlalvaro/php-amqplib/

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

  Sources   Download

LGPL-2.1

The Requires

  • php >=5.3.0
  • ext-bcmath *

 

The Development Requires

rabbitmq queue message

22/12 2013

v2.2.6

2.2.6.0 https://github.com/videlalvaro/php-amqplib/

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

  Sources   Download

LGPL-2.1

The Requires

  • php >=5.3.0
  • ext-bcmath *

 

The Development Requires

rabbitmq queue message

20/12 2013

dev-hhvm

dev-hhvm https://github.com/videlalvaro/php-amqplib/

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

  Sources   Download

LGPL-2.1

The Requires

  • php >=5.3.0
  • ext-bcmath *

 

The Development Requires

rabbitmq queue message

13/12 2013

v2.2.5

2.2.5.0 https://github.com/videlalvaro/php-amqplib/

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

  Sources   Download

LGPL-2.1

The Requires

  • php >=5.3.0
  • ext-bcmath *

 

The Development Requires

rabbitmq queue message

02/12 2013

v2.2.4

2.2.4.0 https://github.com/videlalvaro/php-amqplib/

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

  Sources   Download

LGPL-2.1

The Requires

  • php >=5.3.0
  • ext-bcmath *

 

The Development Requires

rabbitmq queue message

28/11 2013

v2.2.3

2.2.3.0 https://github.com/videlalvaro/php-amqplib/

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

  Sources   Download

LGPL-2.1

The Requires

  • php >=5.3.0
  • ext-bcmath *

 

The Development Requires

rabbitmq queue message

04/10 2013

v2.2.2

2.2.2.0 https://github.com/videlalvaro/php-amqplib/

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

  Sources   Download

LGPL-2.1

The Requires

  • php >=5.3.0
  • ext-bcmath *

 

The Development Requires

rabbitmq queue message

03/10 2013

v2.2.1

2.2.1.0 https://github.com/videlalvaro/php-amqplib/

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

  Sources   Download

LGPL-2.1

The Requires

  • php >=5.3.0
  • ext-bcmath *

 

The Development Requires

rabbitmq queue message

02/10 2013

v2.2.0

2.2.0.0 https://github.com/videlalvaro/php-amqplib/

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

  Sources   Download

LGPL-2.1

The Requires

  • php >=5.3.0
  • ext-bcmath *

 

The Development Requires

rabbitmq queue message

17/06 2013

v2.1.0

2.1.0.0 https://github.com/videlalvaro/php-amqplib/

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

  Sources   Download

LGPL-2.1

The Requires

  • php >=5.3.0
  • ext-bcmath *

 

rabbitmq queue message

13/02 2013

v2.0.2

2.0.2.0 https://github.com/videlalvaro/php-amqplib/

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

  Sources   Download

LGPL-2.1

The Requires

  • php >=5.3.0

 

rabbitmq queue message

27/01 2013

v2.0.1

2.0.1.0 https://github.com/videlalvaro/php-amqplib/

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

  Sources   Download

LGPL-2.1

The Requires

  • php >=5.3.0

 

rabbitmq queue message

17/01 2013

091.x-dev

091.9999999.9999999.9999999-dev https://github.com/videlalvaro/php-amqplib/

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

  Sources   Download

The Requires

  • php >=5.3.0

 

rabbitmq queue message

17/01 2013

v2.0.0

2.0.0.0 https://github.com/videlalvaro/php-amqplib/

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

  Sources   Download

The Requires

  • php >=5.3.0

 

rabbitmq queue message

15/01 2013

v1.2.1

1.2.1.0 https://github.com/videlalvaro/php-amqplib/

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

  Sources   Download

The Requires

  • php >=5.3.0

 

rabbitmq queue message

15/01 2013

v1.2.0

1.2.0.0 https://github.com/videlalvaro/php-amqplib/

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

  Sources   Download

The Requires

  • php >=5.3.0

 

rabbitmq queue message

12/01 2013

v1.1

1.1.0.0 https://github.com/videlalvaro/php-amqplib/

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

  Sources   Download

The Requires

  • php >=5.3.0

 

rabbitmq queue message

28/03 2012

dev-perf_opt

dev-perf_opt https://github.com/videlalvaro/php-amqplib/

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

  Sources   Download

The Requires

  • php >=5.3.0

 

rabbitmq queue message