相关文章推荐
稳重的葫芦  ·  2023-03-19 如何解决 ...·  1 年前    · 
老实的弓箭  ·  React ...·  1 年前    · 
卖萌的勺子  ·  mac 下 ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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
4 require __DIR__ . '/vendor/autoload.php';
6 $dotenv = new Dotenv\Dotenv(__DIR__);
7 $dotenv->load();
9 $sample_msg = "0% chance of rain!";
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 );
19 $amqpChannel = $amqpConnection->channel();
20 $amqpChannel->queue_declare(getenv('SFTP_RABBITMQ_QUEUE'), false, true, false, false);
22 $msg = new AMQPMessage($sample_msg);
23 $channel->basic_publish($msg, '', 'hello');
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
                You say RabbitMQ is configured on port 15672 - are you sure? That's commonly used for the admin interface, with the actual server running on 5672.
– iainn
                Jun 12, 2018 at 12:20
                With port 5672 I get a broken pipe, not connected error. Ok, you're right so I need to figure out what's happening with the broken pipe.
– sdexp
                Jun 12, 2018 at 12:30
  • 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
  • $channel->basic_publish you don't have any variable named $channel, it should be $amqpChannel
  • 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'));
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.