Connect to RabbitMQ from PHP over AMQPS

I work a lot with data these days and queues are a common addition to my applications to move data between applications. At the moment I'm working with RabbitMQ (and loving it!) but I wanted to deploy to a hosted RabbitMQ service and struggled to find examples of doing this over SSL so I thought I'd share what worked for me. Disclaimer: I work for IBM and they own Compose.com, which means I have a "do anything you like!" account there :)

Gather Configuration and Certs

The configuration for RabbitMQ is usually in the format of the URL - from Compose mine looks like this:

amqps://[username]:[password]@sl-eu-lon-2-portal.2.dblayer.com:10406/lj-brilliant-rabbitmq

The PHP library expects to have six separate parameters however:

  • host
  • port
  • username
  • password
  • vhost (e.g. "lj-brilliant-rabbitmq")
  • ssl_options

We also need to get the SSL pieces in order. Compose uses a SSL cert so I have saved that to a file called certrabbit in the same directory as my PHP script.

Now The PHP Code

For this I'm using the php-amqplib package, installed with Composer.

I don't know why the PHP library doesn't like to connect by URL when all the other languages seem to expect this, but all we need to do to use amqps instead of amqp is to use the AMQPSSLConnection instead of the AMQPStreamConnection when we connect to RabbitMQ - and then include the SSL configuration.

Here's the code from my application that allows me to connect:

<?php
// include the composer autoloader
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPSSLConnection;
use PhpAmqpLib\Message\AMQPMessage;

// uncomment this if you need to inspect all AMQP traffic (it's noisy!)
// define('AMQP_DEBUG', true);

$ssl_options = array(
  'capath' => '/etc/ssl/certs',
  'cafile' => './certrabbit', // my downloaded cert file
  'verify_peer' => true,
);

$connection = new AMQPSSLConnection(
  'sl-eu-lon-2-portal.2.dblayer.com',
  10406,
  'lorna',
  'secret',
  'lj-brilliant-rabbitmq',
  $ssl_options);

$channel = $connection->channel();

If successful, you should be able to inspect the $channel and see that it's a PhpAmqpLib\Channel\AMQPChannel object that you can go ahead and work with.

For languages that are not PHP, you can usually supply the URL with the amqps:// prefix but there's also this article on how to connect from a bunch of popular libs.

6 thoughts on “Connect to RabbitMQ from PHP over AMQPS

  1. Hi, I am trying to use your code to connect my rabbitmq using ssl but I am not successful.

    I was wondering if you could explain what cert file you are using to do this.

    Also, on your rabbitmq, do you have the rabbitmq.config set up? If so could I see an example of it?

    • In this case I'm connecting to a remote service, it's RabbitMQ on http://compose.com. To answer your questions: the certificate file is found with the other connection details from compose.com - and so I don't have rabbitmq.config set up for this example because in this case I'm not running the tool locally. Hope that helps!

  2. This was really helpful, I needed an SSL version where the client didn't need to have certificate and it took no time to implement once the server side was working (followed https://www.rabbitmq.com/ssl.html for the server). Here is my sender code:

    [code]
    '',
    'cafile' => '', // no cert required
    'verify_peer' => false,
    );

    #$connection = new AMQPStreamConnection('host', 5671, 'username', 'password');
    $connection = new AMQPSSLConnection('host',5671, 'username','password', '/', $ssl_options);

    $channel = $connection->channel();

    $channel->exchange_declare('exchange_name', 'direct', false, true, false);
    $channel->queue_declare($queue, false, true, false, false);
    $channel->queue_bind($queue, 'exchange_name', $routingkey);

    $data = implode(' ', array_slice($argv, 2));
    if(empty($data)) $data = "Hello World!";

    $msg = new AMQPMessage($data,array('delivery_mode' => 2)); # make message persistent

    $channel->basic_publish($msg, 'exchange_name', $routingkey);

    echo " [x] Sent ",$queue,':',$data," \n";

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

    ?>
    [/code]

  3. Additionally, as of 2-9-2018, having xdebug in operation seems to break SSL transport for php using AMQPClient with SSL. I have no clue why, but it does. If SSL just won't work, try disabling Xdebug.

Leave a Reply

Please use [code] and [/code] around any source code you wish to share.

This site uses Akismet to reduce spam. Learn how your comment data is processed.