0

第一个是从login.php$_POST中接收数据,它通过 调用另一个文件中的函数,并尽可能将其填充到我的登录页面并查看它,但它不会进入消息队列经纪人。require_once$_POSTecho

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

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

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

1 回答 1

0

如果要在函数中修改全局变量,需要使用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

于 2021-10-18T10:38:58.240 回答