Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
php/lib/php-amqplib
25 changes: 25 additions & 0 deletions php/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# RabbitMQ - PHP Examples #

## Requirements ##

To run the examples you need:

- A running RabbitMQ server
- PHP 5.3
- php-amqplib

## Setup ##

To get the php-amqplib library execute the following command inside the present folder:

$ git clone http://github.com/tnc/php-amqplib.git lib/php-amqplib

## Running the Examples ##

To run the examples do in one Terminal:

$ php send.php some message to send

Then in another Terminal try:

$ php receive.php
8 changes: 8 additions & 0 deletions php/config/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
// define('AMQP_DEBUG', true); //enable if you want to see logs.

define('HOST', 'localhost');
define('PORT', 5672);
define('USER', 'guest');
define('PASS', 'guest');
define('VHOST', '/');
21 changes: 21 additions & 0 deletions php/emit_log.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

require_once(__DIR__ . '/lib/php-amqplib/amqp.inc');
include_once(__DIR__ . '/config/config.php');

$connection = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);
$channel = $connection->channel();
$channel->exchange_declare('logs', 'fanout');

$data = implode(' ', array_slice($argv, 1));
if(empty($data)) $data = "info: Hello World!";
$msg = new AMQPMessage($data);

$channel->basic_publish($msg, 'logs');

echo " [x] Sent ", $data, "\n";

$channel->close();
$connection->close();

?>
24 changes: 24 additions & 0 deletions php/new_task.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

require_once(__DIR__ . '/lib/php-amqplib/amqp.inc');
include_once(__DIR__ . '/config/config.php');

$connection = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);
$channel = $connection->channel();

$channel->queue_declare('task_queue', false, true);

$data = implode(' ', array_slice($argv, 1));
if(empty($data)) $data = "Hello World!";
$msg = new AMQPMessage($data,
array('delivery_mode' => 2) # make message persistent
);

$channel->basic_publish($msg, '', 'task_queue');

echo " [x] Sent ", $data, "\n";

$channel->close();
$connection->close();

?>
26 changes: 26 additions & 0 deletions php/receive.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

require_once(__DIR__ . '/lib/php-amqplib/amqp.inc');
include_once(__DIR__ . '/config/config.php');

$connection = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);
$channel = $connection->channel();

$channel->queue_declare('test');

echo ' [*] Waiting for messages. To exit press CTRL+C', "\n";

$callback = function($msg){
echo " [x] Received ", $msg->body, "\n";
};

$channel->basic_consume('test', 'consumer_tag', false, true, false, false, $callback);

while(count($channel->callbacks)) {
$channel->wait();
}

$channel->close();
$connection->close();

?>
29 changes: 29 additions & 0 deletions php/receive_logs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

require_once(__DIR__ . '/lib/php-amqplib/amqp.inc');
include_once(__DIR__ . '/config/config.php');

$connection = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);
$channel = $connection->channel();
$channel->exchange_declare('logs', 'fanout');

list($queue_name, ,) = $channel->queue_declare();

$channel->queue_bind($queue_name, 'logs');

echo ' [*] Waiting for logs. To exit press CTRL+C', "\n";

$callback = function($msg){
echo $msg->body, "\n";
};

$channel->basic_consume($queue_name, 'consumer_tag', false, true, false, false, $callback);

while(count($channel->callbacks)) {
$channel->wait();
}

$channel->close();
$connection->close();

?>
20 changes: 20 additions & 0 deletions php/send.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

require_once(__DIR__ . '/lib/php-amqplib/amqp.inc');
include_once(__DIR__ . '/config/config.php');

$connection = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);
$channel = $connection->channel();

$channel->queue_declare('test');

$msg = new AMQPMessage('Hello World!');

$channel->basic_publish($msg, '', 'test');

echo " [x] Sent 'Hello World!'\n";

$channel->close();
$connection->close();

?>
30 changes: 30 additions & 0 deletions php/worker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

require_once(__DIR__ . '/lib/php-amqplib/amqp.inc');
include_once(__DIR__ . '/config/config.php');

$connection = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);
$channel = $connection->channel();

$channel->queue_declare('task_queue', false, true);

echo ' [*] Waiting for messages. To exit press CTRL+C', "\n";

$callback = function($msg){
echo " [x] Received ", $msg->body, "\n";
sleep(substr_count($msg->body, '.'));
echo " [x] Done", "\n";
$msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
};

$channel->basic_qos(null, 1, null);
$channel->basic_consume('task_queue', 'consumer_tag', false, false, false, false, $callback);

while(count($channel->callbacks)) {
$channel->wait();
}

$channel->close();
$connection->close();

?>