Skip to content
Advertisement

RabbitMQ and Yii console callback error

I am trying to get RabbitMQ working with the Yii console in order to send transactional emails, but I am experiencing problems with getting the PHP-AMQPLib library to work within Yii. My code is below:

<?php
class RabbitMqCommand extends CConsoleCommand {

    public function actionSendMail() {
        require_once ('/htdocs/code/wwwroot/protected/components/php-amqplib/amqp.inc');

        $conn = new AMQPConnection ( 'localhost', 5672, 'guest', '123456', '/' );
        $ch = $conn->channel ();

        $ch->queue_declare ( 'msgs', false, true, false, false );

        $ch->exchange_declare ( 'router', 'direct', false, true, false );

        $ch->queue_bind ( 'msgs', 'router');

        $ch->basic_consume ( 'msgs', 'consumer', false, false, false, false, 'processMessage' );

            // Loop as long as the channel has callbacks registered
        while ( count ( $ch->callbacks ) ) {
            $ch->wait ();
        }

        $ch->close();
        $conn->close();


    }

    public function processMessage($msg) 
        {
           //process and send email
        }

When I try to execute this code from the command line as follows:

php -q /htdocs/code/wwwroot/protected/yiic RabbitMQ SendMail

I receive the following error message:

PHP Error[2]: call_user_func() expects parameter 1 to be a valid callback, function 'processMessage' not found or invalid function name
    in file /htdocs/code/wwwroot/protected/components/php-amqplib/amqp.inc at line 1390
#0 /htdocs/code/wwwroot/protected/components/php-amqplib/amqp.inc(1390): call_user_func()
#1 unknown(0): AMQPChannel->basic_deliver()
#2 /htdocs/code/wwwroot/protected/components/php-amqplib/amqp.inc(167): call_user_func()
#3 /htdocs/code/wwwroot/protected/components/php-amqplib/amqp.inc(275): AMQPChannel->dispatch()
#4 /htdocs/code/wwwroot/protected/commands/RabbitMqCommand.php(29): AMQPChannel->wait()
#5 unknown(0): RabbitMqCommand->actionSendMail()
#6 /htdocs/code/framework/console/CConsoleCommand.php(135): ReflectionMethod->invokeArgs()
#7 /htdocs/code/framework/console/CConsoleCommandRunner.php(63): RabbitMqCommand->run()
#8 /htdocs/code/framework/console/CConsoleApplication.php(88): CConsoleCommandRunner->run()
#9 /htdocs/code/framework/base/CApplication.php(158): CConsoleApplication->processRequest()
#10 /htdocs/code/framework/yiic.php(33): CConsoleApplication->run()
#11 /htdocs/code/wwwroot/protected/yiic.php(7): require_once()
#12 /htdocs/code/wwwroot/protected/yiic(4): require_once()

Any idea what I may be doing wrong?

Advertisement

Answer

This error is happening because processMessage is an instance method of your object but the argument to basic_consume is expected to be a global PHP method.

See this thread for solutions: PHP Call a instance method with call_user_func within the same class

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement