Created
November 7, 2010 20:42
-
-
Save prufrock/666541 to your computer and use it in GitHub Desktop.
A quick example of how to use the PHP AMQP extension from PECL.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Create a connection | |
$cnn = new AMQPConnection(array("host" => "localhost" | |
,"vhost" => "/" | |
,"port" => 5672 | |
,"login" => "guest" | |
,"password" => "guest")); | |
$cnn->connect(); | |
// Declare a new channel | |
$ch = new AMQPChannel($cnn); | |
// Declare a new exchange | |
$ex = new AMQPExchange($ch); | |
$ex->setName("exchange1"); | |
$ex->setType(AMQP_EX_TYPE_DIRECT); | |
$ex->declare(); | |
// Create a new queue | |
$q = new AMQPQueue($ch); | |
$q->setName("testqueue"); | |
$q->declare(); | |
$q->bind($ex->getName(), "routingkey"); | |
// Publish a message to the exchange with the routing key 'routingkey' | |
$ex->publish("test message: " . time(), "routingkey"); | |
// Read from the queue | |
$msg = $q->get(AMQP_AUTOACK); | |
// Display message | |
echo "message received from get: " . print_r($msg->getBody(), true) . "\n"; | |
$ex->delete(); | |
$q->delete(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment