0

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

fpelaezt
  • 1
  • 1
  • Does your server have a firewall? And on RabbitMQ do you have external ports open? – Lulceltech Apr 16 '19 at 18:17
  • Only the default SO firewall using iptables, Port 5672 is open in the RabbitMQ server. Anyway it doesn't seem a network problem because the code is the same and I'm executing the script and the page from the same server, and using the script is working. – fpelaezt Apr 16 '19 at 18:21
  • I also tried using AMQPStreamConnection instead of AMQPConnection, but the result is the same. – fpelaezt Apr 16 '19 at 18:27
  • Well ironically it could be. Localhost != 127.0.0.1 != php cli commands. I'd wager its a network problem before a code problem. I work with rabbit alot, could you post your full snippet for sending a message. – Lulceltech Apr 16 '19 at 18:27
  • Hi @Lulceltech, i edited the question and added the code that I'm using. – fpelaezt Apr 16 '19 at 18:54
  • Why no `try`/`catch` statement around this object creation code? I bet `getMessage()` on the exception object would give you more information than the "debugging" you're currently doing. Or, look in your PHP logs for the uncaught exception. – miken32 Apr 16 '19 at 19:29
  • Your advice was helpful @miken32, I already commented how I fixed it. – fpelaezt Apr 16 '19 at 20:08
  • Possible duplicate of [Fatal error: Call to undefined function mb\_strlen()](https://stackoverflow.com/questions/6419102/fatal-error-call-to-undefined-function-mb-strlen) – miken32 Apr 16 '19 at 20:28

1 Answers1

0

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 ¡¡

fpelaezt
  • 1
  • 1