RabbitMQ: Using PHP to connect to RabbitMQ

This documentation is part of the Getting started guide. View the full guide here: How to use Stackhero for RabbitMQ.

If you are working with PHP, you can use the php-amqplib library to connect to your RabbitMQ instance. Since Stackhero uses TLS encryption (SSL), you will want to use AMQPSSLConnection:

use PhpAmqpLib\Connection\AMQPSSLConnection;

$connection = new AMQPSSLConnection(
  '<XXXXXX>.stackhero-network.com',
  <AMQP_PORT_TLS>,
  'admin',
  '<PASSWORD>',
  '/',
  array()
);

/**
 * @param \PhpAmqpLib\Connection\AbstractConnection $connection
 */
function shutdown($connection)
{
  $connection->close();
}

register_shutdown_function('shutdown', $connection);

Sometimes, your TLS connection might require a Certificate Authority (CA) certificate. While many systems already include this, you can manually download it if needed. Here is how:

  1. Download the certificate from https://letsencrypt.org/certs/isrgrootx1.pem and save it on your server.
  2. Then, you can connect using PHP with the downloaded certificate as follows:
$sslOptions = array(
  'cafile' => realpath(__DIR__ . '/isrgrootx1.pem'),
);

$connection = new AMQPSSLConnection(
  '<XXXXXX>.stackhero-network.com',
  <AMQP_PORT_TLS>,
  'admin',
  '<PASSWORD>',
  '/',
  $sslOptions
);