donglikuang8145 2019-04-16 18:12
浏览 561

RabbitMQ发布可以从控制台运行,但不能从浏览器运行

I have a simple PHP code that sends a message to a RabbitMQ queue, when I execute it directly from the console (#php script.php) the message is published correctly, but when I try to open the same script using my browser (http://localhost/script.php) the message is not sent. All previous lines are executed but the page stucks in the line

$connection = new AMQPConnection('$ipaddress', 5672, '$user', '$password');

Any ideas of why is this happening ?

CODE: script.php

<?php
include_once('rabbitmq_function.php');
$result = rabbitmq_send("test");
print_r($result);

CODE: rabbitmq_function.php

<?php
require_once '/usr/share/php/PhpAmqpLib/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Connection\AMQPConnection;
use PhpAmqpLib\Message\AMQPMessage;

function rabbitmq_send($data)
{
    print_r("Before AMQP Connection");
    $connection = new AMQPConnection('10.0.0.8', 5672, 'guest', 'guest');
    print_r("After AMQP Connection");
    $channel = $connection->channel();
    $channel->queue_declare('task_queue', false, true, false, false);
    $msg = new AMQPMessage(
        $data,
        array('delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT)
    );
    $channel->basic_publish($msg, '', 'task_queue');
    $channel->close();
    $connection->close();
    return true;
}

From CLI: I get the two debug messages (Before and After), and the return "1" value. And also I'm seeing a new message in the RabbitMQ management. From Browser: I get only printed the "Before AMQP Connection" message and no new message in the queue

  • 写回答

1条回答

  • dst2007 2019-04-16 20:07
    关注

    Thanks @mikens32 for the advice. I had use the try/cath clause but I didn't get any message, anyway looking in the apache error_log I could see that this was the error:

    [Tue Apr 16 18:51:47 2019] [error] [client 10.0.2.2] PHP Fatal error:  Call to 
    undefined function PhpAmqpLib\\Wire\\mb_strlen() in 
    /usr/share/php/PhpAmqpLib/Wire/AMQPReader.php on line 63
    

    I don't know why using the script was working, but I found that this package (mbstring which has the mb_strlen function) is not enable by default. So I installed, restart apache and now it's working.

    Thanks guys ¡¡

    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料