phprabbitmqrabbitmq-exchangephp-amqplib

implode of array results in RabbitMQ receiving empty msg body yet other publishers show the body


The first one is receiving data from $_POST in a login.php which calls a function from another file through require_once, and the $_POST is populated as I can echo it to my login page and see it, but it doesn't make it to the queue on the message broker.

Here is the code that RabbitMQ receives but the message is empty:

<?php
    require_once __DIR__ . '/vendor/autoload.php';
    use PhpAmqpLib\Connection\AMQPStreamConnection;
    use PhpAmqpLib\Message\AMQPMessage;
    include 'host_info.php';
    $connection = new AMQPStreamConnection($address, 5672, $admin, $adminpass);
    $channel = $connection->channel();
    $channel->queue_declare('login', false, false, false, false);

    $data;
    function setMessage($arr) {
        $data = implode(',', $arr);

        echo $data;
        if (empty($data)) {
            echo 'Please input your email and/or password.';
            return;
        }
        return;
    }

    $msg = new AMQPMessage($data, array('delivery_mode'=> AMQPMEssage::DELIVERY_MODE_PERSISTENT));
    $channel->basic_publish($msg,'', 'login');



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

And here is the code from another file that RabbitMQ receives and does have a body:

<?php
    require_once __DIR__ . '/vendor/autoload.php';
    use PhpAmqpLib\Connection\AMQPStreamConnection;
    use PhpAmqpLib\Message\AMQPMessage;
    include 'host_info.php';
    $connection = new AMQPStreamConnection($address, 5672, $admin, $adminpass);
    $channel = $connection->channel();
    $channel->queue_declare('login', false, false, false, false);

    $data = implode(' ', array_slice($argv, 1));
    if (empty($data)) {
        $data = 'Hello World!';
    }
    $msg = new AMQPMessage($data, array('delivery_mode'=> AMQPMessage::DELIVERY_MODE_PERSISTENT));
    $channel->basic_publish($msg, '', 'login');

    echo '[x] Sent' , $data, "\n";
    $channel->close();
    $connection->close();
?>

If there are any errors in these it is because I had to handtype out the second because it was on a VM and the bidirectional clipboard does not work.

edit: here is the code from login.php

 <?php
    session_start();
    include 'host_info.php';

    $email;
    $pass;
    $arr;

    if (isset($_POST['email']) && isset($_POST['password'])) {
        require ('send_login_request.php');
        $email = $_POST['email'];

        $pass = $_POST['password'];

        $arr = array($email, $pass);
        setMessage($arr);
    }
?>

Solution

  • If you want to modify a global variable in function, you need to use the global keyword

        $data;
        function setMessage($arr) {
            global $data;
            $data = implode(',', $arr);
    
            echo $data;
            if (empty($data)) {
                echo 'Please input your email and/or password.';
                return;
            }
            return;
        }
    

    https://www.php.net/manual/en/language.variables.scope.php