数组的内爆导致 RabbitMQ 接收到空的消息正文,但其他发布者显示正文

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

第一个是在 login.php 中从 $_POST 接收数据,它通过 require_once 从另一个文件调用函数,然后$_POST 已尽我所能填充 echo 它到我的登录页面并看到它,但它没有进入消息代理的队列。

这里是 RabbitMQ 收到但消息为空的代码:

<?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();
?>

这里是来自另一个文件的代码,RabbitMQ 收到并且确实有一个正文:

<?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();
?>

如果其中有任何错误,那是因为我不得不手写第二个,因为它在 VM 上并且双向剪贴板不起作用。

编辑:这是来自 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);
    }
?>

如果要在函数中修改全局变量,需要使用global关键字

    $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