How should PHP's AMQP be used?

Source: Internet
Author: User
Tags ack rabbitmq
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

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.