Over a million developers have joined DZone.

Connect to RabbitMQ From PHP Over AMQPS

DZone's Guide to

Connect to RabbitMQ From PHP Over AMQPS

Check out this step-by-step guide to put together the puzzle of deploying to a hosted RabbitMQ service.

· Integration Zone ·
Free Resource

SnapLogic is the leading self-service enterprise-grade integration platform. Download the 2018 GartnerMagic Quadrant for Enterprise iPaaS or play around on the platform, risk free, for 30 days.

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:


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 an 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:

// 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(

$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.

Download A Buyer's Guide to Application and Data Integration, your one-stop-shop for research, checklists, and explanations for an application and data integration solution.

rabbitmq ,library ,configuration ,php ,ssl ,parameters ,script ,applications

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}