RabbitMQ: Using PHP to connect to RabbitMQ

This documentation is part of the Getting Started guide. You can view the complete guide here: How to use Stackhero for RabbitMQ.

If you are developing in PHP, the php-amqplib library allows you to connect to your RabbitMQ instance. Since Stackhero uses TLS (SSL) encryption, it is recommended 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);

In some cases, your TLS connection may require a Certificate Authority (CA) certificate. Many systems already include this, but you can manually download it if needed. Here’s how:

  1. Download the certificate from https://letsencrypt.org/certs/isrgrootx1.pem and save it on your server.
  2. Then, 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
);