Skip to content

Instantly share code, notes, and snippets.

@prufrock
Created November 7, 2010 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prufrock/666541 to your computer and use it in GitHub Desktop.
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.
<?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