svn checkout http://php-rabbit.googlecode.com/svn/trunk/ phpize && ./configure --with-rabbit && make && sudo make install
extension = rabbit.so
// declare Exchange $cnn = new AMQPConnect(array ('port'=>5673, 'login' => 'sector1' )); $exchange = new AMQPExchange($cnn); $exchange->declare('ex_name', 'topic',AMQP_DURABLE ); // exchange delete $exchange = new AMQPExchange($cnn, 'ex_name'); $res = $exchange->delete(); // exeption if exchange not exist // publishing $msg = "my news in the Spb..."; $cnn = new AMQPConnect(array ('port'=>5673, 'login' => 'sector1' )); $exchange = new AMQPExchange($cnn); $exchange->declare( 'ex_name', 'topic'); $exchange->bind('mylogin','spb.*'); $res = $exchange->publish( $msg, 'spb.news'); //Queue declare $cnn = new AMQPConnect(array ('port'=>5673, 'login' => 'sector1' )); $queue = new AMQPQueue($cnn); $queue->declare('chat_12', AMQP_AUTEDELETE | AMQP_DURABLE); //Queue delete $queue = new AMQPQueue($cnn,'chat_12'); $queue->delete(); //Queue purge $queue = new AMQPQueue($cnn,'chat_12'); $queue->purge(); //bind to exchange $queue = new AMQPQueue(AMQPConnect(), 'mylogin'); $queue->declare(); $queue->bind('ex_estate','*.spb'); //unbind $queue = new AMQPQueue(AMQPConnect() ); $queue->declare('mylogin'); $queue->unbind('ex_estate','*.spb'); // consume $i=0; $queue = new AMQPQueue(AMQPConnect()); $n = $queue->declare('mylogin'); $queueMessages = $queue->consume( $n ); foreach($queueMessages as $item){ $i++; echo "$i.$item"; } // get queue items $queue = new AMQPQueue(AMQPConnect(), 'mylogin'); $i = 5; // get 5 or less messages $res = true; while ( $i-- ){ $res = $queue->get(); if ($res['count']<0) break; echo "$i.{$res['msg']}<br>\n"; } /* res= Array ( [count] => 3 - how messages in queue ; if the count= -1, the queue is null, if thecount = 0 - the message is last [msg] => message1 (string) ) */
use consume( queue_name, msg_count) method with parameter count less or equal fact message length. if use the count less as fact, the nonreaded message will canceled!
Open logical connection
AMQPConnect::AMQPConnect(array params)
parms (all is optional):
Exceptio if connection is fail.
Example: $cnn = new AMQPConnect(array ('port'=>5673, 'login' => 'sector1', 'vhost'=>'v1' ) );
AMQPExchange::AMQPExchange(AMQPConnect cnn, string name) name - name of exchange, optional
//Example: $cnn = new AMQPConnect(array ('port'=>5673, 'login' => 'sector1' )); $exchange = new AMQPExchange($cnn, 'ex_name');
proto bool AMQPEexchange::declare( string name , string type=direct, bit params );
name - name of exchange, optional type - type of exchangeа, only: direct, topic or fanout, optional, the direct is default params (optional): * AMQP_PASSIVE * AMQP_DURABLE * AMQP_AUTODELETE * AMQP_INTERNAL
return result of operation
//Example: $cnn = new AMQPConnect(array ('port'=>5673, 'login' => 'sector1' )); $exchange = new AMQPExchange($cnn); $exchange->declare('ex_name', 'topic',AMQP_DURABLE );
proto bool AMQPExchange::delete( string name , bit params );
name - name of exchange, optional params (optional): * AMQP_IFUNUSED
return result of operation
//Example: $cnn = new AMQPConnect(array ('port'=>5673, 'login' => 'sector1' )); $exchange = new AMQPExchange($cnn, 'ex_name'); $res = $exchange->delete();
proto bool AMQPExchange::bind( string queueName, string routingKey );
queueName - name of queue key - routing-key
return result of operation
//Example: $msg = "my news text of msg....."; $cnn = new AMQPConnect(array ('port'=>5673, 'login' => 'sector1' )); $exchange = new AMQPExchange($cnn, 'ex_name'); $exchange->bind('mylogin','spb.news') $res = $exchange->publish( $msg, 'spb.news');
proto bool AMQPExchange::publish( string msg, string key ,bit parms, array properies );
Publish message with routing key for exchangeа of topic or direct type
msg - msg key - routing-key, optional for fanout type params (optional): * AMQP_MANDATORY * AMQP_IMMEDIATE properies (optional): is array of properies: * Content-type * Content-encoding * message_id * user_id default : Content-type = text/plain
return result of operation
//Example: $msg = "my news text of msg....."; $cnn = new AMQPConnect(array ('port'=>5673, 'login' => 'sector1' )); $exchange = new AMQPExchange($cnn, 'ex_name'); $res = $exchange->publish( $msg, 'spb.news');
AMQPQueue::AMQPQueue( AMQPConnection cnn, string name )
name - name of queue, optional
Declare queue proto int AMQPQueue::declare( string name, bit params )
name - name of queue, optional params (optional): * AMQP_AUTODELETE (default) * AMQP_DURABLE * AMQP_PASSIVE * AMQP_EXCLUSIVE
return the count of queue items
//Example: $cnn = new AMQPConnect(array ('port'=>5673, 'login' => 'sector1' )); $queue = new AMQPQueue($cnn,'chat_12'); $queue->declare('chat_12', AMQP_AUTEDELETE | AMQP_DURABLE);
proto bool AMQPQueue::delete( string name, bit params )
name - name of queue, optional params (optional): * AMQP_IFUNUSED * AMQP_IFEMPTY
return result of operation
//Example: $queue = new AMQPQueue(new AMQPConnect(),'chat_12'); $queue->delete();
proto bool AMQPQueue::bind( string exchangeName, string routingKey );
name - name of exchange routingKey - routing key
//Example: $queue = new AMQPQueue(AMQPConnect(), 'mylogin'); $queue->declare(); $queue->bind('ex_estate','*.spb');
proto array AMQPQueue::consume( int n );
get from queue array of n messages, unreaded messages is cancel. n - count of received messages params: * AMQP_NOLOCAL * AMQP_NOACK * AMQP_EXCLUSIVE
Attention! count of received messages must be less that count of queue messages else the client will wait the receiving new messages.
//Example: $i=0; $queue = new AMQPQueue(AMQPConnect()); $n = $queue->declare('mylogin'); $queueMessages = $queue->consume( $n ); foreach($queueMessages as $item){ $i++; echo "$i.$item"; }
proto bool AMQPQueue::cancel( string consumer_tag )
consumer_tag - optional
return result of operation
//Example: $queue = new AMQPQueue(new AMQPConnect(),'chat_12'); $queue->cancel();
proto bool AMQPQueue::unbind( string exchangeName, string routingKey );
name - name of exchangeа routingkey - routing key
return result of operation
proto bool AMQPQueue::purge( string name )
name - name of queue, optional return result of operation
//Example: $queue = new AMQPQueue(new AMQPConnect()); $queue->purge('chat_12');
proto array AMQPQueue::get( string name , bit params)
name - name of queue, optional params (optional): * AMQP_NOASK (default)
*return* array: msg - curent message exchange - the exchange name, from sent message routing_key - the meassage''s routing key count - count messages into queue, if: count = 0 - the messages is last count = -1 queue is null, the key "msg" is absen array(4) { ["count"]=> int(0) ["routing_key"]=> string(4) "test" ["exchange"]=> string(3) "ex6" ["msg"]=> string(14) "test message 3" } //Example: // read 5 or less messages $queue = new AMQPQueue(AMQPConnect(), 'mylogin'); $i = 5; $res = true; while ( $i-- ){ $res = $queue->get(); if ($res['count']<0) break; echo "$i.{$res['msg']}<br>\n"; }