Use PHP to try Rabbitmq (AMQP extensions) to send and receive messages

Source: Internet
Author: User
Tags ack rabbitmq

Consumer: Receiving Messages

Logic:
Create a connection--create a channel--> switch--Create a queue--bind switch/queue/route keys receive Message

<?php/************************************** php amqp (RabbitMQ) demo-consumer* author:linvo* date:2012/7/30***** Configuration Information $conn_args = Array (' host ' = ' 192.168.1.93 ', ' port ' = ' 5672 ', ' log ' In ' = ' guest ', ' password ' = ' guest ', ' vhost ' = '/'), $e _name = ' e_linvo '; Switch name $q_name = ' Q_linvo '; Queue name $k_route = ' Key_1 '; Route key//Create connection and channel$conn = new Amqpconnection ($conn _args), if (! $conn->connect ()) {die ("Cannot connect to the Brok Er!\n ");} $channel = new Amqpchannel ($conn);//create switch $ex = new Amqpexchange ($channel); $ex->setname ($e _name); $ex->settype ( Amqp_ex_type_direct); Direct type $ex->setflags (amqp_durable); Persistent echo "Exchange Status:" $ex->declare (). " \ n ";//Create queue $q = new Amqpqueue ($channel); $q->setname ($q _name); $q->setflags (amqp_durable); Persistent echo "Message total:". $q->declare (). " \ n ";//Bind the switch with the queue and specify the routing key Echo ' queue Bind: '. $q->bind ($e _name, $k _route)."    \ n ";//blocking mode receives the message echo" message:\n "; while (True) {$q->consume (' ProcessMessage '); $q->consume (' ProcessMessage ', amqp_autoack); Auto ack reply} $conn->disconnect ();/*** consumption callback function * Processing message */function ProcessMessage ($envelope, $queue) {$msg = $envelope    GetBody (); echo $msg. " \ n "; Processing Messages $queue->ack ($envelope->getdeliverytag ()); Send ACK reply manually}

Producer: Send Message

Logic:
Create a connection--create a channel--> switch Object--send a message Continue reading →

Posted in System Architecture | Tagged amqp, RabbitMQ

Install RABBITMQ PHP Extensions AMQP (original) under WindowsPosted on 2015/03/28

Download the corresponding version from PHP official HTTP://PECL.PHP.NET/PACKAGE/AMQP, I use 1.4.0 version (Http://pecl.php.net/package/amqp/1.4.0/windows)
Select the appropriate extension DLL based on the PHP version currently in use, and after the download is a compressed package with two DLL extensions (Php_amqp.dll and Rabbitmq.1.dll).

My environment is 64-bit, php5.5.12. So it's http://windows.php.net/downloads/pecl/releases/amqp/1.4.0/php_amqp-1.4.0-5.5-ts-vc11-x64.zip.

1. Place the Php_amqp.dll in the PHP ext directory, then modify the php.ini file to add two lines to the last side of the file

12 [amqp]extension=php_amqp.dll

2. Place the Rabbitmq.1.dll file in the root directory of PHP (that is, the parent directory of the EXT directory) and modify the Apache Httpd.con file to add a line at the end of the file

1 LoadFile  "d:/wamp/bin/php/php5.5.12/rabbitmq.1.dll"

The path here is modified according to the situation, I use the Wampserver software here.

3. Restart Apache and view the phpinfo information. Just see the AMQP word.

Use PHP to try Rabbitmq (AMQP extensions) to send and receive messages

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.