videlalvaro / php-amqplib
Formerly videlalvaro/php-amqplib. This library is a pure PHP implementation of the AMQP protocol. It's been tested against RabbitMQ.
Requires
- php: ^8.0
- ext-mbstring: *
- ext-sockets: *
- phpseclib/phpseclib: ^3.0|^4.0.1
Requires (Dev)
- ext-curl: *
- guzzlehttp/guzzle: ^7.0
- phpunit/phpunit: ^9.5|^10
- squizlabs/php_codesniffer: ^3.6
Suggests
None
Provides
None
Conflicts
- php: 7.4.0 - 7.4.1
Replaces
- videlalvaro/php-amqplib: v3.8.0
- v3.8.0
- v3.7.5
- v3.7.4
- v3.7.3
- v3.7.2
- v3.7.1
- v3.7.0
- v3.6.2
- v3.6.1
- v3.6.0
- v3.5.4
- v3.5.3
- v3.5.2
- v3.5.1
- v3.5.0
- v3.4.0
- v3.3.1
- v3.3.0
- v3.2.0
- v3.1.2
- v3.1.1
- v3.1.0
- dev-master / 3.0.x-dev
- v3.0.0
- v3.0.0-rc2
- v3.0.0-rc1
- 2.x-dev
- v2.12.3
- 2.12.2
- v2.12.1
- v2.12.0
- v2.11.3
- v2.11.2
- v2.11.1
- v2.11.0
- v2.10.1
- v2.10.0
- v2.10.0-rc1
- v2.9.2
- v2.9.1
- v2.9.0
- v2.9.0-rc2
- v2.9.0-rc1
- v2.9.0-beta.1
- v2.8.2-rc3
- v2.8.2-rc2
- v2.8.2-rc1
- v2.8.1
- v2.8.1-rc3
- v2.8.1-rc2
- v2.8.1-rc1
- v2.8.0
- v2.8.0-rc1
- v2.7.3
- v2.7.2.1
- v2.7.2
- v2.7.1
- v2.7.1-rc2
- v2.7.1-rc1
- v2.7.0
- v2.7.0-rc1
- v2.6.3
- v2.6.2
- v2.6.1
- v2.6.0
- v2.5.2
- v2.5.1
- v2.5.0
- v2.4.1
- v2.4.0
- v2.3.0
- v2.2.6
- v2.2.5
- v2.2.4
- v2.2.3
- v2.2.2
- v2.2.1
- v2.2.0
- v2.1.0
- v2.0.2
- v2.0.1
- v2.0.0
- v1.2.1
- v1.2.0
- v1.1
This package is auto-updated.
Last update: 2026-12-02 20:56:30 UTC
README
php-amqplib is a pure PHP implementation of the AMQP 0-9-1 protocol. It is primarily tested against RabbitMQ.
This library was originally used for the PHP examples in RabbitMQ in Action and the official RabbitMQ tutorials.
Table of Contents
- Features
- Project Status & Maintainers
- Requirements
- Installation
- Quick Start
- Advanced Usage
- Debugging
- Contributing
Features
- Pure PHP: No C extensions required.
- AMQP 0-9-1 Support: Compatible with RabbitMQ 3.x and later.
- RabbitMQ Extensions:
- Exchange to Exchange Bindings
- Basic Nack
- Publisher Confirms
- Consumer Cancel Notify
- Connection Pooling: Compatible with AMQProxy for lower connection churn.
- Interop: Compatible with enqueue/amqp-lib for AMQP interop.
Project Status & Maintainers
The project is currently maintained by Ramūnas Dronga, Luke Bakken, and several VMware engineers.
We welcome contributions! Please see CONTRIBUTING.md for details.
Note: This project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.
Requirements
- PHP 8.0 or higher
- RabbitMQ 3.0 or later
Installation
Ensure you have Composer installed, then run:
composer require php-amqplib/php-amqplib
This fetches the library into your vendor folder. You can then include the autoloader in your PHP files:
require_once __DIR__.'/vendor/autoload.php';
Import the necessary classes:
use PhpAmqpLib\Connection\AMQPStreamConnection; use PhpAmqpLib\Message\AMQPMessage;
Quick Start
To try the library, open two terminals and start a consumer and publisher.
Terminal 1 (Consumer):
cd demo
php amqp_consumer.php
Terminal 2 (Publisher):
cd demo php amqp_publisher.php "some text to publish"
You should see the message arrive in the first terminal. To stop the consumer, send a quit message:
php amqp_publisher.php quit
For a non-blocking consumer example, see demo/amqp_consumer_non_blocking.php.
Advanced Usage
Batch Publishing
If you are publishing many messages to the same exchange/routing key, you can use batch_basic_publish to improve performance.
$ch = $connection->channel(); // Queue messages $msg = new AMQPMessage('body 1'); $ch->batch_basic_publish($msg, $exchange); $msg2 = new AMQPMessage('body 2'); $ch->batch_basic_publish($msg2, $exchange); // Send all queued messages at once $ch->publish_batch();
You can decide when to flush the batch (e.g., every 50 messages or 100) based on your throughput requirements.
Optimized Message Publishing
If you are publishing many messages with identical properties (e.g., content type, delivery mode), reuse the AMQPMessage instance and update only the body. This avoids re-encoding the properties header for every message.
$properties = [ 'content_type' => 'text/plain', 'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT, ]; // Create the message once $msg = new AMQPMessage('', $properties); // Loop and update only the body foreach ($data as $body) { $msg->setBody($body); $ch->basic_publish($msg, $exchange); }
Truncating Large Messages
AMQP does not limit message size, but PHP's memory_limit might be hit if a very large message is received before the consumer callback is called.
You can set a body size limit on the channel:
$channel->setBodySizeLimit(1024 * 1024); // 1MB limit
- Messages exceeding this limit are truncated.
- The message object will have
$is_truncatedset totrueand$body_sizereflecting the original size. - To disable truncation, call
setBodySizeLimit(0).
Connection Recovery
Since php-amqplib is single-threaded, you should implement connection recovery in your application logic using exception handling.
Common connection exceptions include:
PhpAmqpLib\Exception\AMQPConnectionClosedExceptionPhpAmqpLib\Exception\AMQPIOException\RuntimeException\ErrorException
Example Recovery Loop:
$connection = null; $channel = null; while (true) { try { if (!$connection) { $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest'); $channel = $connection->channel(); } // Subscribe to single queue $channel->basic_consume('my_queue', '', false, false, false, false, $callback); // Wait and process all incoming messages in an endless loop until connection exception or manual stop using self::stopConsume() $channel->consume(); } catch (\PhpAmqpLib\Exception\AMQPException $e) { // Log and clean up if ($channel) { $channel->close(); } if ($connection) { $connection->close(); } $connection = null; $channel = null; // Wait before retrying sleep(1); } catch (\Exception $e) { // Handle application errors differently // ... } }
See demo/connection_recovery_consume.php for a full example.
Unix Signals
If the PCNTL extension is installed (common on Linux), signals are dispatched between consumer loops. You can register custom handlers:
$pcntlHandler = function ($signal) { switch ($signal) { case \SIGTERM: case \SIGINT: // Cleanup and exit exit(0); case \SIGHUP: // Restart logic break; } }; pcntl_signal(\SIGTERM, $pcntlHandler); pcntl_signal(\SIGINT, $pcntlHandler);
To disable signal handling, define the constant before loading the library:
define('AMQP_WITHOUT_SIGNALS', true);
Signal-based Heartbeat
If using PCNTL (Linux), you can register a heartbeat sender to keep connections alive:
use PhpAmqpLib\Heartbeat\PCNTLHeartbeatSender; $sender = new PCNTLHeartbeatSender($connection); $sender->register(); // ... your code ... $sender->unregister();
Debugging
To enable protocol-level debugging, define this constant before running your script:
define('AMQP_DEBUG', true);
API Documentation
http://php-amqplib.github.io/php-amqplib/
Tutorials
- Official RabbitMQ Tutorials
amqp_ha_consumer.php: Mirrored queuesamqp_consumer_exclusive.php: Fanout exchanges with exclusive queuesamqp_consumer_fanout_{1,2}.php: Fanout exchanges with named queuesamqp_consumer_pcntl_heartbeat.php: Signal-based heartbeat usagebasic_get.php: Usingbasic_getto poll for messages
Contributing
Please see CONTRIBUTING.md for details on how to run tests and submit changes.
License
This project is licensed under the LGPL 2.1. See LICENSE for more information.