Install AMQP extension and AMQP message sending and receiving in php in linux
First, check whether the extension has been installed:
Php-m | grep amqp if not, install the amqp extension
Official installation instructions: http://www.php.net/manual/zh/amqp.installation.php
Download rabbitmq-c v0.2: https://github.com/alanxz/rabbitmq-c/tags
Or:
wget https://github.com/alanxz/rabbitmq-c/tarball/0.2 tar zxvf 0.2 cd alanxz-rabbitmq*
Download the latest codegen kit: https://github.com/rabbitmq/rabbitmq-codegen/downloads
wget https://github.com/rabbitmq/rabbitmq-codegen/tarball/master tar zxvf master mv rabbitmq-* codegen autoreconf -i ./configure && make && make install
Extended compilation:
Wget http://pecl.php.net/get/amqp-1.0.0.tgz tar zxvf amqp-1.0.0.tgz cd amqp-1.0.0/usr/local/php/bin/phpize. /configure -- with-php-config =/usr/local/bin/php-config -- with-amqp # make & make install in your own phpconfig path
If OK is added to php. ini: extension = amqp. so
Php-m | grep amqp if the amqp extension appears, the installation is complete.
'2017. 168.0.12 ', 'port' => '123', 'login' => 'guest', 'password' => 'guest ', 'vhost' => '/'); $ e_name = "exchange"; $ q_name = "queue"; $ k_route = "route. key "; $ conn = new AMQPConnection ($ conn_args); if ($ conn-> connect () {echo" Established a connection to the broker \ n ";} else {echo "Cannot connect to the broker \ n";} // Your message $ message = "hello word! ". Time (); // Create channel $ channel = new AMQPChannel ($ conn); // create exchange $ ex = new AMQPExchange ($ channel ); $ ex-> setName ($ e_name); // create a name $ ex-> setType (AMQP_EX_TYPE_DIRECT); $ ex-> setFlags (AMQP_DURABLE | AMQP_AUTODELETE); echo "exchange status: ". $ ex-> declare (); echo "\ n"; // Create a queue $ q = new AMQPQueue ($ channel ); // add $ q-> setName ($ q_name); $ q-> setFlags (AMQP_DURABLE | AMQP_AUTODELETE) if the queue name does not exist. // echo "queue status :". $ q-> declare (); echo "\ n"; echo 'queue bind :'. $ q-> bind ($ e_name, $ k_route); // bind your queue to routingKeyecho "\ n"; $ channel-> startTransaction (); // for ($ I = 0; $ I <500; ++ $ I) {} echo "send :". $ ex-> publish ($ message, $ k_route); // send your message to echo "\ n" by specifying the routingKey; $ channel-> commitTransaction (); $ conn-> disconnect (); receive the message:
'2017. 168.0.12 ', 'port' => '123', 'login' => 'guest', 'password' => 'guest ', 'vhost' => '/'); $ e_name = "exchange"; $ q_name = "queue"; $ k_route = "route. key "; $ conn = new AMQPConnection ($ conn_args); if (! $ Conn-> connect () {die ("Cannot connect to the broker! \ N ") ;}$ channel = new AMQPChannel ($ conn); $ ex = new AMQPExchange ($ channel); $ ex-> setName ($ e_name ); $ ex-> setType (AMQP_EX_TYPE_DIRECT); $ ex-> setFlags (AMQP_DURABLE | AMQP_AUTODELETE); echo "Exchange Status :". $ ex-> declare (). "\ n"; $ q = new AMQPQueue ($ channel); $ q-> setName ($ q_name); $ q-> setFlags (AMQP_DURABLE | AMQP_AUTODELETE ); // echo "Message Total :". $ q-> declare (). "\ n"; echo 'queue Bind :'. $ q-> bind ($ e_name, $ k_route ). "\ n"; echo "Message: \ n"; while (True) {$ q-> consume ('processmessage '); // $ q-> consume ('processmessage', AMQP_AUTOACK);} $ conn-> disconnect (); function processMessage ($ envelope, $ queue) {$ msg = $ envelope-> getBody (); echo $ msg. "\ n"; $ queue-> ack ($ envelope-> getDeliveryTag ());}