How to run RabbitMQ with PHP

RabbitMQ has support for several programming languages. This support is in the form of client libraries, which are plentiful and widely used by developers everywhere. This tutorial covers how to connect to RabbitMQ with PHP. All samaple code is available on Github.

Before you go any further in this article, be sure to get started with a free RabbitMQ plan at CloudAMQP. to be able to easily follow along.

PHP is a popular server-side scripting language with an officially recognized RabbitMQ library. The php-amqplib package is a pure PHP implementation of the AMQP 0.9.1 protocol. In this article, we will show how to install and use php-amqplib through a RabbitMQ PHP example.

Installing amqp-lib

A PHP package manager is required to install amqp-lib. This tutorial uses Composer as the package manager.

Install the package manager using the directions on their website and run:

composer require php-amqplib/php-amqplib

Composer installed amqp-lib to your vendor folder. Open your favorite editor to start building your PHP application.

Publishing to RabbitMQ

The amqp-lib library implements the AMQP protocol. As a barebones implementation, there are no additional features. You must create publishers and subscribers from scratch.

Create an exchange and a queue and publish a message as follows:

<?php
  require('vendor/autoload.php');
  use PhpAmqpLib\Connection\AMQPStreamConnection;
  use PhpAmqpLib\Connection\AMQPSSLConnection;
  use PhpAmqpLib\Message\AMQPMessage;
  
  $url_str = getenv('CLOUDAMQP_URL')
    or exit("CLOUDAMQP_URL not set");
  $url = parse_url($url_str);
  $vhost = substr($url['path'], 1);

  if($url['scheme'] === "amqps") {
    $ssl_opts = array(
      'capath' => '/etc/ssl/certs'
    );
    $connection = new AMQPSSLConnection($url['host'], 5671, $url['user'], $url['pass'], $vhost, $ssl_opts);
  } else {
    $connection = new AMQPStreamConnection($url['host'], 5672, $url['user'], $url['pass'], $vhost);
  }

  $channel = $connection->channel();

  $channel->exchange_declare('test_exchange', 'direct', false, false, false);
  $channel->queue_declare('test_queue', false, false, false, false);
  $channel->queue_bind('test_queue', 'test_exchange', 'test_key');

  $msg = new AMQPMessage('Hello World!');
  $channel->basic_publish($msg, 'test_exchange', 'test_key');

  echo " [x] Sent 'Hello World!' to test_exchange / test_queue.\n";

  $channel->close();
  $connection->close();
?>

We created the test_exchange and bound the test_queue to the exchange using the routing key test_key. We also sent a basic "hello world" message to the queue.

Consuming from RabbitMQ

With our test message in test_queue, we should next create a consumer that prints our message to the console. Subscribe to test_queue and process the message as follows:

<?php
  require('vendor/autoload.php');
  use PhpAmqpLib\Connection\AMQPStreamConnection;
  use PhpAmqpLib\Connection\AMQPSSLConnection;
  use PhpAmqpLib\Message\AMQPMessage;

  $url = parse_url(getenv('CLOUDAMQP_URL'));
  $vhost = substr($url['path'], 1);

  if($url['scheme'] === "amqps") {
    $ssl_opts = array(
        'capath' => '/etc/ssl/certs'
    );
    $connection = new AMQPSSLConnection($url['host'], 5671, $url['user'], $url['pass'], $vhost, $ssl_opts);
  } else {
    $connection = new AMQPStreamConnection($url['host'], 5672, $url['user'], $url['pass'], $vhost);
  }

  $channel = $connection->channel();

  $callback = function ($msg) {
    echo ' [x] Received ', $msg->body, "\n";
  };

  $channel->basic_consume('test_queue', '', false, true, false, false, $callback);

  while ($channel->is_consuming()) {
    $channel->wait();
  }

  $channel->close();
  $connection->close();
?>

The "hello world" message should appear in your console. We now have a working RabbitMQ publisher-subscriber model.

Connecting to CloudAMQP in PHP

CloudAMQP is a fully-managed RabbitMQ broker. As such, these examples showed you how to send and receive messages using php-amqplib and your cloud broker. To connect to CloudAMQP, replace the URLs in the examples with your CloudAMQP URL.

CloudAMQP - industry leading RabbitMQ as a service

Start your managed cluster today. CloudAMQP is 100% free to try.

13,000+ users including these smart companies