I installed the AMQP extension for RABBITMQ and PHP, but did not know how to use it, I searched the web for tutorials, but not too understand,
This is the send.php file,
$conf=[ 'host'=>'localhost', 'port'=>'5672', 'login'=>'guest', 'password'=>'guest', 'vhost'=>'/'];$connect=new \AMQPConnection($conf);if(!$connect->connect()){ die('connetc error');}$message = "TEST MESSAGE! 测试消息!";$channel=new AMQPChannel($connect);$exchange=new AMQPExchange($channel);$exchange->setName('zblExchange');$queue = new AMQPQueue($channel);$queue->setName('zblQueue');for($i=0; $i<5; ++$i){ echo "Send Message:".$exchange->publish($message, 'zblQueue')."\n";}//$channel->commitTransaction(); //提交事务$connect->disconnect();
This is the receive.php file
$conf =[' host ' = ' localhost ', ' port ' = ' 5672 ', ' login ' = ' guest ', ' Password ' + ' guest ', ' vhost ' and ' = ' /']; $connect =new amqpconnection ($conf), if (! $connect->connect ()) {die (' connetc error ');} $channel =new Amqpchannel ($connect), $exchange =new amqpexchange ($channel); $exchange->setname (' Zblexchange '); $ Exchange->settype (amqp_ex_type_direct);//echo "Exchange Status:". $exchange->declare (). " \ n "; $queue = new Amqpqueue ($channel); $queue->setname (' Zblqueue '); $queue->setflags (amqp_durable); Persistent echo "Message total:". $queue->declare (). " \ n "; Echo ' Queue Bind: '. $queue->bind (' Zblexchange ', ' Zblqueue ')." \ n "; echo" message:\n ";//while (True) {$queue->consume (' ProcessMessage '); echo ' Hello '; $q->consume (' ProcessMessage ', amqp_autoack); Auto ACK response//} $connect->disconnect (), function ProcessMessage ($envelope, $queue) {$msg = $envelope->getbody (); echo $msg. " \ n "; Processing Messages $queue->ack ($envelope->getdeliverytag ()); Send ack reply manually}
There is no problem accessing send.php using the browser, and then accessing receive.php is 504 Gateway timeout, Fatal error when accessing send.php using the command line Amqpconnection class can't find it, I don't know what's going on, I can't find the AMQP extension installed when I use php-m, but when I use phpinfo to access it through the browser it shows that AMQP is loaded, The use of the environment is CentOS 7,nginx 1.6.3,php 5.6.9, excuse me, what is the problem here?
Reply content:
I installed the AMQP extension for RABBITMQ and PHP, but did not know how to use it, I searched the web for tutorials, but not too understand,
This is the send.php file,
$conf=[ 'host'=>'localhost', 'port'=>'5672', 'login'=>'guest', 'password'=>'guest', 'vhost'=>'/'];$connect=new \AMQPConnection($conf);if(!$connect->connect()){ die('connetc error');}$message = "TEST MESSAGE! 测试消息!";$channel=new AMQPChannel($connect);$exchange=new AMQPExchange($channel);$exchange->setName('zblExchange');$queue = new AMQPQueue($channel);$queue->setName('zblQueue');for($i=0; $i<5; ++$i){ echo "Send Message:".$exchange->publish($message, 'zblQueue')."\n";}//$channel->commitTransaction(); //提交事务$connect->disconnect();
This is the receive.php file
$conf =[' host ' = ' localhost ', ' port ' = ' 5672 ', ' login ' = ' guest ', ' Password ' + ' guest ', ' vhost ' and ' = ' /']; $connect =new amqpconnection ($conf), if (! $connect->connect ()) {die (' connetc error ');} $channel =new Amqpchannel ($connect), $exchange =new amqpexchange ($channel); $exchange->setname (' Zblexchange '); $ Exchange->settype (amqp_ex_type_direct);//echo "Exchange Status:". $exchange->declare (). " \ n "; $queue = new Amqpqueue ($channel); $queue->setname (' Zblqueue '); $queue->setflags (amqp_durable); Persistent echo "Message total:". $queue->declare (). " \ n "; Echo ' Queue Bind: '. $queue->bind (' Zblexchange ', ' Zblqueue ')." \ n "; echo" message:\n ";//while (True) {$queue->consume (' ProcessMessage '); echo ' Hello '; $q->consume (' ProcessMessage ', amqp_autoack); Auto ACK response//} $connect->disconnect (), function ProcessMessage ($envelope, $queue) {$msg = $envelope->getbody (); echo $msg. " \ n "; Processing Messages $queue->ack ($envelope->getdeliverytag ()); Send ack reply manually}
There is no problem accessing send.php using the browser, and then accessing receive.php is 504 Gateway timeout, Fatal error when accessing send.php using the command line Amqpconnection class can't find it, I don't know what's going on, I can't find the AMQP extension installed when I use php-m, but when I use phpinfo to access it through the browser it shows that AMQP is loaded, The use of the environment is CentOS 7,nginx 1.6.3,php 5.6.9, excuse me, what is the problem here?
The installed AMQP extension is not found when using Php-m, but is displayed when phpinfo is accessed through the browser.
This is because the AMQP extension is only loaded in FPM, and the CLI script does not load in the same way, configuration is good, as for 504, I think it's probably your FPM is not configured well
Please refer to: http://segmentfault.com/a/1190000002963223