RMQ Consumer (PHP)

  1. Install PHP: Make sure you have PHP installed on your system. You can download the latest version of PHP from the official PHP website and follow the installation instructions for your operating system. Alternative and easier way to automatically install PHP with Chocolatey: To install PHP using Chocolatey (choco) package manager on Windows, follow these steps:

    1. Install Chocolatey:

      • Open PowerShell as Administrator.

      • Run the following command to install Chocolatey:

        Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
      • Allow the installation to complete.

    2. Install PHP:

      • Open PowerShell or a new command prompt.

      • Run the following command to install PHP using Chocolatey:

        choco install php
      • Allow the installation to complete.

    3. Verify the PHP installation:

      • Open a new command prompt or PowerShell.

      • Run the following command to check if PHP is installed and get the PHP version:

        codephp -v
      • If PHP is installed successfully, you should see the PHP version information.

  2. Install Composer: Composer is a dependency management tool for PHP. You'll need Composer to install the required dependencies for the code. You can download Composer from the official Composer website and follow the installation instructions for your operating system.

  3. Set up the project: Create a new directory for your project and navigate to that directory in your terminal or command prompt.

  4. Create a composer.json file: In the project directory, create a new file called composer.json and paste the following content into it:

{
    "require": {
        "php-amqplib/php-amqplib": "^3.5.4"
    }
}
  1. Install dependencies: Run the following command in your terminal or command prompt to install the required dependencies:

composer install

This command will download and install the necessary packages, including the php-amqplib/php-amqplib package.

  1. Replace <FilesPath>: In the code, you'll notice the placeholder <FilesPath>. Replace it with the actual path where you want to save the JSON files.

  2. Run the code: Save the PHP code in a file with a .php extension (e.g., consumer.php). Open your terminal or command prompt, navigate to the project directory, and run the following command:

php consumer.php

The code will establish a connection to RabbitMQ, consume messages from the queue specified by "_99_", process the messages, and save the JSON files to the specified directory.

Make sure you have the correct RabbitMQ server details (hostname, port, username, password, virtual host) and adjust them in the code accordingly.

Note: The code assumes you have the php-amqplib/php-amqplib package installed via Composer. If you encounter any issues related to missing dependencies, ensure that you've completed the steps to install the dependencies using Composer. Code:

<?php

require_once __DIR__.'/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;

// Set up RabbitMQ connection
$connection = new AMQPStreamConnection("HostName", 5672, "email", "password", "VHOST");
$channel = $connection->channel();

// Set up consumer to receive messages from the queue
$channel->basic_consume("_QUEUE_", '', false, true, false, false, function ($msg) {
    // Convert the message body to a string
    $body = $msg->getBody();

    // Parse the JSON string into an object
    $updateObject = json_decode($body);

    // Serialize the object back to a string, with indentation and line breaks
    $beautifiedUpdate = json_encode($updateObject, JSON_PRETTY_PRINT);

    // Get the current date and time in the UTC +0 time zone
    $utcTime = new DateTime('now', new DateTimeZone('UTC'));

    // Convert the DateTime object to a string in the desired format
    $dateTimeString = $utcTime->format('Y-m-d\TH-i-s\Z');

    // Generate the file path using the formatted date and time string
    $filePath = "C:/Users/tomer.a/Downloads/Consumer/files/" . $dateTimeString . ".json";

    // Save the beautified JSON string to a file
    file_put_contents($filePath, $beautifiedUpdate);
});

while ($channel->is_consuming()) {
    $channel->wait();
}

$channel->close();
$connection->close();

?>

Last updated