I installed the amqp extension of rabbitmq and php, but I don't know how to use it. I searched for the tutorial on the Internet, but I didn't quite understand it. This is send. PHP file, {code ...} this is receive. PHP file {code ...} use the browser to first access send. php is fine. Access receive again. php... I installed the amqp extension of rabbitmq and php, but I don't know how to use it. I searched for the tutorial on the Internet, but I didn't understand it too well,
This is the send. php file,
$ Conf = ['host' => 'localhost', 'Port' => '000000', 'login' => 'guest ', 'Password' => 'guest ', 'vhost' => '/']; $ connect = new \ AMQPConnection ($ conf); if (! $ Connect-> connect () {die ('connetc error');} $ message = "test message! Test message! "; $ Channel = new AMQPChannel ($ connect); $ exchange = new AMQPExchange ($ channel); $ exchange-> setName ('zblexchang '); $ queue = new AMQPQueue ($ channel); $ queue-> setName ('zblqueue '); for ($ I = 0; $ I <5; ++ $ I) {echo "Send Message :". $ exchange-> publish ($ message, 'zblqueue '). "\ n" ;}// $ channel-> commitTransaction (); // submit the transaction $ connect-> disconnect ();
This is the receive. php file.
$ Conf = ['host' => 'localhost', 'Port' => '000000', 'login' => 'guest ', 'Password' => 'guest ', 'vhost' => '/']; $ 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 ('zblexchang', 'zblqueue '). "\ n"; echo "Message: \ n"; // while (True) {$ queue-> consume ('processmessage'); // echo 'hello '; // $ q-> consume ('processmessage', AMQP_AUTOACK); // automatic ACK response //} $ connect-> disconnect (); function processMessage ($ envelope, $ queue) {$ msg = $ envelope-> getBody (); echo $ msg. "\ n"; // process the message $ queue-> ack ($ envelope-> getDeliveryTag (); // manually send ACK response}
Use the browser to first access send. php is fine. Access receive again. php is 504 gateway timeout. Use the command line to access send. the AMQPConnection class cannot be found due to a fatal error in php. I don't know what is going on. I cannot find the installed amqp extension when using php-m, however, when using phpinfo to access through a browser, it is displayed that amqp is loaded, and the Environment is centos 7, nginx 1.6.3, php 5.6.9. What is the problem here?
Reply content:
I installed the amqp extension of rabbitmq and php, but I don't know how to use it. I searched for the tutorial on the Internet, but I didn't understand it too well,
This is the send. php file,
$ Conf = ['host' => 'localhost', 'Port' => '000000', 'login' => 'guest ', 'Password' => 'guest ', 'vhost' => '/']; $ connect = new \ AMQPConnection ($ conf); if (! $ Connect-> connect () {die ('connetc error');} $ message = "test message! Test message! "; $ Channel = new AMQPChannel ($ connect); $ exchange = new AMQPExchange ($ channel); $ exchange-> setName ('zblexchang '); $ queue = new AMQPQueue ($ channel); $ queue-> setName ('zblqueue '); for ($ I = 0; $ I <5; ++ $ I) {echo "Send Message :". $ exchange-> publish ($ message, 'zblqueue '). "\ n" ;}// $ channel-> commitTransaction (); // submit the transaction $ connect-> disconnect ();
This is the receive. php file.
$ Conf = ['host' => 'localhost', 'Port' => '000000', 'login' => 'guest ', 'Password' => 'guest ', 'vhost' => '/']; $ 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 ('zblexchang', 'zblqueue '). "\ n"; echo "Message: \ n"; // while (True) {$ queue-> consume ('processmessage'); // echo 'hello '; // $ q-> consume ('processmessage', AMQP_AUTOACK); // automatic ACK response //} $ connect-> disconnect (); function processMessage ($ envelope, $ queue) {$ msg = $ envelope-> getBody (); echo $ msg. "\ n"; // process the message $ queue-> ack ($ envelope-> getDeliveryTag (); // manually send ACK response}
Use the browser to first access send. php is fine. Access receive again. php is 504 gateway timeout. Use the command line to access send. the AMQPConnection class cannot be found due to a fatal error in php. I don't know what is going on. I cannot find the installed amqp extension when using php-m, however, when using phpinfo to access through a browser, it is displayed that amqp is loaded, and the Environment is centos 7, nginx 1.6.3, php 5.6.9. What is the problem here?
The installed amqp extension cannot be found when php-m is used, but
This is because the amqp extension is only loaded in fpm, but not in cli Script Mode. Just configure it. As for 504, I think it is very likely that your fpm is not configured properly.
See http://segmentfault.com/a/1190000002963223