AMQP
在线手册:中文 英文
PHP手册

范例

Example #1 AMQP Example

<?php

// Create a connection
$cnn = new AMQPConnection();
$cnn->connect();

// Create a channel
$ch = new AMQPChannel($cnn);

// Declare a new exchange
$ex = new AMQPExchange($ch);
$ex->declare('exchange1'AMQP_EX_TYPE_FANOUT);

// Create a new queue
$q = new AMQPQueue($ch);
$q->declare('queue1');

// Bind it on the exchange to routing.key
$ex->bind('queue1''routing.key');

// Publish a message to the exchange with a routing key
$ex->publish('message''routing.key');

// Read from the queue
$msg $q->consume();

?>

AMQP
在线手册:中文 英文
PHP手册
PHP手册 - N: 范例

用户评论:

info at eeasoftware dot com (26-Feb-2012 04:54)

I spent several hours looking for some examples of sending messages to worker queues vs. fan queues.  The documentation isn't too clear and I had to consult many sources, so here are simple methods for both types of 'producers'.  For brevity I've left out 'safety' checks in the code dealing with whether or not a connection was established, error checking, cleanup, etc.

As further clarification, I'm using rabbitmq as my service, with java daemons running on the server as 'consumers'.  These examples are current with the new 1.0.0 stable AMQP release in February of 2012.

<?php

//set your connection arguments and connect to the server
$conn_args = array('host' => 'your_host', 'port' => 'your_host_port', 'login' => 'your_username', 'password' => 'your_password');
$conn = new AMQPConnection($conn_args);
$conn->connect();

//create your message
$message = 'Hello World!';

//create a channel and exchange
$channel = new AMQPChannel($conn);
$exchange = new AMQPExchange($channel);

//Here is where the code splits for the different types of queues:

//-------------------------------------------------
//For Fan Type Queues (One message will be consumed by many consumers - each on a queue of their own)

//set the exchange name and publish the message
$exchange->setName('exchange_name');
$exchange->publish($message, 'your_routing_key');

//for Fan Queue calls not bound to a particular key, the 'routing_key' will simply be ignored

//-------------------------------------------------
//For Worker Type Queues (Many consumers are listening to a single queue, and a single message will be passed only to the next available consumer waiting in line)

//start a transaction
$channel->startTransaction();

//publish your message with 'your_queue_name' as the 'routing_key'
$exchange->publish($message, 'your_queue_name');

//commit your transaction
$channel->commitTransaction();

//-------------------------------------------------

//psuedo code:
//clean up $channel; - probably just unset($channel) will do.
//clean up $exchange; - probably just unset($exchange) will do.

//disconnect from server
$conn->disconnect();

?>

kurt at surfmerchants dot com (24-May-2011 05:05)

The docs for this extension are a little vague, so I'm adding a few things I found helpful in getting this to work.

Helpful reading:
* http://en.wikipedia.org/wiki/AMQP
* http://www.rabbitmq.com/getstarted.html

Helpful tool:
/usr/sbin/rabbitmqctl list_queues

Here I've split the usage example into a sender and receiver, because that is much more useful (at least to me):

send.php :
#!/usr/bin/php -q
<?php
// config
$exchangeName = 'myexchange';
$routingKey = 'routing.key';
$message = $argv[1];

// connect
$connection = new AMQPConnection();
$connection->connect();

// setup exchange
$ex = new AMQPExchange($connection);
$ex->declare($exchangeName, AMQP_EX_TYPE_FANOUT);

// Publish a message to the exchange with our routing key
$ex->publish($message, $routingKey);

// disconnect
$connection->disconnect();

?>

receive.php:
#!/usr/bin/php -q
<?php
// config
$exchangeName = 'myexchange';
$routingKey = 'routing.key';
$queueName = 'myqueue';

// connect
$connection = new AMQPConnection();
$connection->connect();

// setup our queue
$q = new AMQPQueue($connection);
$q->declare($queueName);

// Bind it on the exchange to routing.key
$q->bind($exchangeName, $routingKey);

// show the message
print_r($q->get());

// disconnect
$connection->disconnect();

?>

The first time you run "./send.php 'hello world'", then "./receive.php", you will not get the message, because the receiver creates the queue. That's OK. While you *can* create the queue in the sender, this is not so useful, because it's really the receivers that read the queue, and I've found that various clients are very picky about the details of how the queue was created (for example, if I create the queue in my php sender, then try to listen with a python receiver, it throws errors, but if I let the python receiver create the queue, all is well).
So, the second time you send a message (after running receive.php once), the queue will exist, and you will then get the message the next time you run receive.

Another thing I noticed is that I'm using $q->get() instead of $q->consume(), because the latter seems to segfault (at least on my CentOS 5.5 system).