CloudAMQP with PHP Getting started

To access CloudAMQP (or any RabbitMQ/AMQP server) from PHP php-amqplib is a good choice. The library php-amqplib was used for the PHP examples of RabbitMQ in Action and in the official RabbitMQ tutorials.

Code example Publish and Subscribe

The following code snippet show how to connect, publish and consume a message via CloudAMQP.

define('AMQP_DEBUG', true);
use PhpAmqpLib\Connection\AMQPConnection;
use PhpAmqpLib\Message\AMQPMessage;

* Create a connection to RabbitMQ

$url = parse_url(getenv('CLOUDAMQP_URL'));
$conn = new AMQPConnection(
          $url['host'], //host - CloudAMQP_URL
          5672,         //port - port number of the service, 5672 is the default
          $url['user'], //user - username to connect to server
          $url['pass'], //password - password to connecto to the server
          substr($url['path'], 1) //vhost
$ch = $conn->channel();

$exchange = '';
$queue = 'basic_get_queue';
          $queue, //queue name
          false,  //passive -  check whether an exchange exists without modifying server state
          true,   //durable - RabbitMQ will never lose the queue if a crash occurs
          false,  //exclusive - if queue only will be used by one connection
          false   //autodelete - queue is deleted when last consumer unsubscribes

$ch->exchange_declare($exchange, 'direct', true, true, false);
$ch->queue_bind($queue, $exchange);

$msg_body = 'the body';
$msg = new AMQPMessage($msg_body, array('content_type' => 'text/plain', 'delivery_mode' => 2));
$ch->basic_publish($msg, $exchange);

$retrived_msg = $ch->basic_get($queue);


The full code example can be found at

Alternative clients

PECL AMQP library built on top of the RabbitMQ C client
A complete AMQP protocol implementation and a set of libraries for writing AMQP producers and consumers.
Vorpal Bunny
Vorpal Bunny is an experimental rabbitmq-jsonrpc-channel PHP driver designed to allow expedited single call HTTP delivery for Basic.Deliver calls to RabbitMQ.