DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Inheritance in PHP: A Simple Guide With Examples
  • The Blue Elephant in the Room: Why PHP Should Not Be Ignored Now or Ever
  • Laravel for Beginners: An Overview of the PHP Framework
  • Is PHP Still the Best Language in 2024?

Trending

  • What’s Got Me Interested in OpenTelemetry—And Pursuing Certification
  • Monoliths, REST, and Spring Boot Sidecars: A Real Modernization Playbook
  • Creating a Web Project: Caching for Performance Optimization
  • Analyzing Techniques to Provision Access via IDAM Models During Emergency and Disaster Response
  1. DZone
  2. Coding
  3. Languages
  4. Connect to RabbitMQ From PHP Over AMQPS

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.

By 
Lorna Mitchell user avatar
Lorna Mitchell
·
Aug. 16, 16 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
9.7K Views

Join the DZone community and get the full member experience.

Join For Free

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

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

PHP

Published at DZone with permission of Lorna Mitchell, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Inheritance in PHP: A Simple Guide With Examples
  • The Blue Elephant in the Room: Why PHP Should Not Be Ignored Now or Ever
  • Laravel for Beginners: An Overview of the PHP Framework
  • Is PHP Still the Best Language in 2024?

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!