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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog