RabbitMQ 和 Yii 控制台回调错误

发布于 2025-01-03 16:44:02 字数 2483 浏览 2 评论 0原文

我正在尝试让 RabbitMQ 与 Yii 控制台配合使用来发送事务电子邮件,但是在让 PHP-AMQPLib 库在 Yii 中工作时遇到问题。我的代码如下:

<?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
        }

当我尝试从命令行执行此代码时,如下所示:

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

我收到以下错误消息:

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()

知道我可能做错了什么吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蹲墙角沉默 2025-01-10 16:44:02

发生此错误的原因是 processMessage 是对象的实例方法,但 basic_consume 的参数应为全局 PHP 方法。

请参阅此线程以获取解决方案: PHP 在同一个类中使用 call_user_func 调用实例方法

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

多情癖 2025-01-10 16:44:02

请将这一行:更改

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

为:

$channel->basic_consume('msgs', 'consumer', false, false, false, false, [new RabbitMqCommand,'process_message']);

Please change this line:

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

to this :

$channel->basic_consume('msgs', 'consumer', false, false, false, false, [new RabbitMqCommand,'process_message']);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文