RabbitMQ 和 Yii 控制台回调错误
我正在尝试让 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
发生此错误的原因是
processMessage
是对象的实例方法,但basic_consume
的参数应为全局 PHP 方法。请参阅此线程以获取解决方案: PHP 在同一个类中使用 call_user_func 调用实例方法
This error is happening because
processMessage
is an instance method of your object but the argument tobasic_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
请将这一行:更改
为:
Please change this line:
to this :