phpphp-amqplib

php-amqplib connection error


I'm just doing a test to try and get RabbitMQ to work with php-amqplib. I've just edited the question so that it's using the right port. Any ideas on this?

This is my PHP file...

1 #!/usr/bin/env php
2 <?php
3
4 require __DIR__ . '/vendor/autoload.php';
5
6 $dotenv = new Dotenv\Dotenv(__DIR__);
7 $dotenv->load();
8
9 $sample_msg = "0% chance of rain!";
10
11 $amqpConnection = new \PhpAmqpLib\Connection\AMQPStreamConnection(
12     getenv('RABBITMQ_HOST'),
13     getenv('RABBITMQ_PORT'),
14     getenv('RABBITMQ_USER'),
15     getenv('RABBITMQ_PASSWORD'),
16     getenv('RABBITMQ_VHOST')
17 );
18
19 $amqpChannel = $amqpConnection->channel();
20 $amqpChannel->queue_declare(getenv('SFTP_RABBITMQ_QUEUE'), false, true, false, false);
21
22 $msg = new AMQPMessage($sample_msg);
23 $channel->basic_publish($msg, '', 'hello');
24
25 echo " [x] Sent $sample_msg\n";

This is my .env...

RABBITMQ_HOST=dev.website.co.uk
RABBITMQ_PORT=5672 // ammended
RABBITMQ_VHOST=/
RABBITMQ_LOGIN=xxxxxx
RABBITMQ_PASSWORD=xxxxxx

SFTP_RABBITMQ_QUEUE=my_test_queue

This is the composer.json...

{
    "name": "neil/sftp-user-create",
    "type": "project",
    "require": {
        "php-amqplib/php-amqplib": "^2.7",
        "vlucas/phpdotenv": "^2.4"
        },
    "license": "proprietary",
    "authors": [
        {
             "name": "Me",
             "email": "my@email-address.com"
                                                             }
     ]
}

This is the error I get...

PHP Fatal error:  Uncaught PhpAmqpLib\Exception\AMQPRuntimeException: Broken pipe or closed connection in /home/neil/PhpstormProjects/sftp-user-create/vendor/php-amqplib/php-amqplib/PhpAmqpLib/Wire/IO/StreamIO.php:214
Stack trace:
#0 /home/neil/PhpstormProjects/sftp-user-create/vendor/php-amqplib/php-amqplib/PhpAmqpLib/Wire/AMQPReader.php(149): PhpAmqpLib\Wire\IO\StreamIO->read(7)
#1 /home/neil/PhpstormProjects/sftp-user-create/vendor/php-amqplib/php-amqplib/PhpAmqpLib/Wire/AMQPReader.php(106): PhpAmqpLib\Wire\AMQPReader->rawread(7)
#2 /home/neil/PhpstormProjects/sftp-user-create/vendor/php-amqplib/php-amqplib/PhpAmqpLib/Connection/AbstractConnection.php(522): PhpAmqpLib\Wire\AMQPReader->read(7)
#3 /home/neil/PhpstormProjects/sftp-user-create/vendor/php-amqplib/php-amqplib/PhpAmqpLib/Connection/AbstractConnection.php(570): PhpAmqpLib\Connection\AbstractConnection->wait_frame(0)
#4 /home/neil/PhpstormProjects/sftp-user-create/vendor/php-amqplib/php-amqplib/PhpAmqpLib/Channel/AbstractChannel.php(225): PhpAmqpLib\Connection\Abstr in /home/neil/PhpstormProjects/sftp-user-create/vendor/php-amqplib/php-amqplib/PhpAmqpLib/Wire/IO/StreamIO.php on line 214

Solution

  • You have several issues in your code:

    1. In you .env file you declared RABBITMQ_LOGIN yet in your code you are using RABBITMQ_USER, that gives you empty login and "Broken pipe" errors after you fixed connection port as @iainn mentioned
    2. $channel->basic_publish you don't have any variable named $channel, it should be $amqpChannel
    3. In basic_publish your last argument should be the name of the queue you want to send message to, so it should be the same as in queue_declare that is getenv('SFTP_RABBITMQ_QUEUE')

    Fix this in your code:

    $amqpConnection = new \PhpAmqpLib\Connection\AMQPStreamConnection(
         getenv('RABBITMQ_HOST'),
         getenv('RABBITMQ_PORT'),
         getenv('RABBITMQ_LOGIN'), // Use _LOGIN as declared in .env
         getenv('RABBITMQ_PASSWORD'),
         getenv('RABBITMQ_VHOST')
    );
    

    (...)

    // Use $amqpChannel as you declared it earlier in code
    // And use the same queue name in last parameter as you used in queue_declare
    $amqpChannel->basic_publish($msg, '', getenv('SFTP_RABBITMQ_QUEUE'));