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