Skip to content

Instantly share code, notes, and snippets.

@videlalvaro
Created November 3, 2014 01:54
Show Gist options
  • Select an option

  • Save videlalvaro/07792cf97069c884e166 to your computer and use it in GitHub Desktop.

Select an option

Save videlalvaro/07792cf97069c884e166 to your computer and use it in GitHub Desktop.
<?php
# usage: php add_exchange.php 'images'
include(__DIR__ . '/config.php');
use PhpAmqpLib\Connection\AMQPConnection;
$conn = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);
$ch = $conn->channel();
$exchange = $argv[1];
$ch->exchange_declare($exchange, 'x-modulus-hash', false, true, false);
<?php
require_once __DIR__.'/../vendor/autoload.php';
define('HOST', 'localhost');
define('PORT', 5672);
define('USER', 'guest');
define('PASS', 'guest');
define('VHOST', '/');
//If this is enabled you can see AMQP output on the CLI
define('AMQP_DEBUG', false);
<?php
# usage: php consumer.php 'images'
include(__DIR__ . '/config.php');
use PhpAmqpLib\Connection\AMQPConnection;
$queue = $argv[1];
$conn = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);
$ch = $conn->channel();
function process_message($msg)
{
echo "\n--------\n";
echo $msg->body;
echo "\n--------\n";
$msg->delivery_info['channel']->
basic_ack($msg->delivery_info['delivery_tag']);
}
/*
queue: Queue from where to get the messages
consumer_tag: Consumer identifier
no_local: Don't receive messages published by this consumer.
no_ack: Tells the server if the consumer will acknowledge the messages.
exclusive: Request exclusive consumer access, meaning only this consumer can access the queue
nowait:
callback: A PHP Callback
*/
$ch->basic_consume($queue, '', false, false, false, false, 'process_message');
function shutdown($ch, $conn)
{
$ch->close();
$conn->close();
}
register_shutdown_function('shutdown', $ch, $conn);
// Loop as long as the channel has callbacks registered
while (count($ch->callbacks)) {
$ch->wait();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment