Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
530 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# PHP code for RabbitMQ tutorials based on AMQP interop | ||
|
||
Here you can find PHP code examples from [RabbitMQ | ||
tutorials](http://www.rabbitmq.com/getstarted.html) adopted to [amqp interop](https://github.com/queue-interop/queue-interop#amqp-interop) | ||
These examples will work with any amqp interop compatible transports such as [enqueue/amqp-ext](https://github.com/php-enqueue/enqueue-dev/blob/master/docs/transport/amqp.md), [enqueue/amqp-bunny](https://github.com/php-enqueue/enqueue-dev/blob/master/docs/transport/amqp_bunny.md), [enqueue/amqp-lib](https://github.com/php-enqueue/enqueue-dev/blob/master/docs/transport/amqp_lib.md) | ||
|
||
To successfully use the examples you will need a running RabbitMQ server. | ||
|
||
## Requirements | ||
|
||
### PHP 5.5+ | ||
|
||
You need `PHP 5.5` and one of the amqp interop compatible transport. | ||
|
||
|
||
### Composer | ||
|
||
Then [install Composer](https://getcomposer.org/download/) per instructions on their site. | ||
|
||
### Client Library | ||
|
||
Then you can, for example, install `enqueue/amqp-bunny` using [Composer](http://getcomposer.org). | ||
|
||
To do that install Composer and add it to your path, then run the following command | ||
inside this project folder: | ||
|
||
```bash | ||
$ composer require enqueue/amqp-bunny | ||
``` | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
// composer require enqueue/amqp-bunny | ||
require_once __DIR__.'/vendor/autoload.php'; | ||
|
||
use Enqueue\AmqpBunny\AmqpConnectionFactory; | ||
use Interop\Amqp\AmqpTopic; | ||
|
||
$config = [ | ||
'host' => 'localhost', | ||
'port' => 5672, | ||
'user' => 'guest', | ||
'pass' => 'guest', | ||
]; | ||
|
||
$connection = new AmqpConnectionFactory($config); | ||
$context = $connection->createContext(); | ||
|
||
$topic = $context->createTopic('logs'); | ||
$topic->setType(AmqpTopic::TYPE_FANOUT); | ||
|
||
$context->declareTopic($topic); | ||
|
||
$data = implode(' ', array_slice($argv, 1)); | ||
if (empty($data)) { | ||
$data = 'info: Hello World!'; | ||
} | ||
$message = $context->createMessage($data); | ||
|
||
$context->createProducer()->send($topic, $message); | ||
|
||
echo ' [x] Sent ', $data, "\n"; | ||
|
||
$context->close(); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
// composer require enqueue/amqp-bunny | ||
require_once __DIR__.'/vendor/autoload.php'; | ||
|
||
use Enqueue\AmqpBunny\AmqpConnectionFactory; | ||
use Interop\Amqp\AmqpTopic; | ||
|
||
$config = [ | ||
'host' => 'localhost', | ||
'port' => 5672, | ||
'user' => 'guest', | ||
'pass' => 'guest', | ||
]; | ||
|
||
$connection = new AmqpConnectionFactory($config); | ||
$context = $connection->createContext(); | ||
|
||
$topic = $context->createTopic('direct_logs'); | ||
$topic->setType(AmqpTopic::TYPE_DIRECT); | ||
|
||
$context->declareTopic($topic); | ||
|
||
$severity = isset($argv[1]) && !empty($argv[1]) ? $argv[1] : 'info'; | ||
|
||
$data = implode(' ', array_slice($argv, 2)); | ||
if (empty($data)) { | ||
$data = 'Hello World!'; | ||
} | ||
|
||
$message = $context->createMessage($data); | ||
$message->setRoutingKey($severity); | ||
|
||
$context->createProducer()->send($topic, $message); | ||
|
||
echo ' [x] Sent ',$severity,':',$data," \n"; | ||
|
||
$context->close(); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
// composer require enqueue/amqp-bunny | ||
require_once __DIR__.'/vendor/autoload.php'; | ||
|
||
use Enqueue\AmqpBunny\AmqpConnectionFactory; | ||
use Interop\Amqp\AmqpTopic; | ||
|
||
$config = [ | ||
'host' => 'localhost', | ||
'port' => 5672, | ||
'user' => 'guest', | ||
'pass' => 'guest', | ||
]; | ||
|
||
$connection = new AmqpConnectionFactory($config); | ||
$context = $connection->createContext(); | ||
|
||
$topic = $context->createTopic('topic_logs'); | ||
$topic->setType(AmqpTopic::TYPE_TOPIC); | ||
|
||
$context->declareTopic($topic); | ||
|
||
$routing_key = isset($argv[1]) && !empty($argv[1]) ? $argv[1] : 'anonymous.info'; | ||
|
||
$data = implode(' ', array_slice($argv, 2)); | ||
if (empty($data)) { | ||
$data = 'Hello World!'; | ||
} | ||
|
||
$message = $context->createMessage($data); | ||
$message->setRoutingKey($routing_key); | ||
|
||
$context->createProducer()->send($topic, $message); | ||
|
||
echo ' [x] Sent ',$routing_key,':',$data," \n"; | ||
|
||
$context->close(); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
// composer require enqueue/amqp-bunny | ||
require_once __DIR__.'/vendor/autoload.php'; | ||
|
||
use Enqueue\AmqpBunny\AmqpConnectionFactory; | ||
use Interop\Amqp\AmqpMessage; | ||
use Interop\Amqp\AmqpQueue; | ||
|
||
$config = [ | ||
'host' => 'localhost', | ||
'port' => 5672, | ||
'user' => 'guest', | ||
'pass' => 'guest', | ||
]; | ||
|
||
$connection = new AmqpConnectionFactory($config); | ||
$context = $connection->createContext(); | ||
|
||
$queue = $context->createQueue('task_queue'); | ||
$queue->addFlag(AmqpQueue::FLAG_DURABLE); | ||
|
||
$context->declareQueue($queue); | ||
|
||
$data = implode(' ', array_slice($argv, 1)); | ||
if (empty($data)) { | ||
$data = 'Hello World!'; | ||
} | ||
$message = $context->createMessage($data); | ||
$message->setDeliveryMode(AmqpMessage::DELIVERY_MODE_PERSISTENT); | ||
|
||
$context->createProducer()->send($queue, $message); | ||
|
||
echo ' [x] Sent ', $data, "\n"; | ||
|
||
$context->close(); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
// composer require enqueue/amqp-bunny | ||
require_once __DIR__.'/vendor/autoload.php'; | ||
|
||
use Enqueue\AmqpBunny\AmqpConnectionFactory; | ||
use Interop\Amqp\AmqpConsumer; | ||
|
||
$config = [ | ||
'host' => 'localhost', | ||
'port' => 5672, | ||
'user' => 'guest', | ||
'pass' => 'guest', | ||
'receive_method' => 'basic_consume', | ||
]; | ||
|
||
$connection = new AmqpConnectionFactory($config); | ||
$context = $connection->createContext(); | ||
|
||
$queue = $context->createQueue('hello'); | ||
$context->declareQueue($queue); | ||
|
||
$consumer = $context->createConsumer($queue); | ||
$consumer->addFlag(AmqpConsumer::FLAG_NOACK); | ||
|
||
echo ' [*] Waiting for messages. To exit press CTRL+C', "\n"; | ||
|
||
while (true) { | ||
if ($message = $consumer->receive()) { | ||
echo ' [x] Received ', $message->getBody(), "\n"; | ||
} | ||
} | ||
|
||
$context->close(); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
// composer require enqueue/amqp-bunny | ||
require_once __DIR__.'/vendor/autoload.php'; | ||
|
||
use Enqueue\AmqpBunny\AmqpConnectionFactory; | ||
use Interop\Amqp\AmqpConsumer; | ||
use Interop\Amqp\AmqpTopic; | ||
use Interop\Amqp\Impl\AmqpBind; | ||
|
||
$config = [ | ||
'host' => 'localhost', | ||
'port' => 5672, | ||
'user' => 'guest', | ||
'pass' => 'guest', | ||
'receive_method' => 'basic_consume', | ||
]; | ||
|
||
$connection = new AmqpConnectionFactory($config); | ||
$context = $connection->createContext(); | ||
|
||
$topic = $context->createTopic('logs'); | ||
$topic->setType(AmqpTopic::TYPE_FANOUT); | ||
|
||
$context->declareTopic($topic); | ||
|
||
$queue = $context->createTemporaryQueue(); | ||
|
||
$context->bind(new AmqpBind($topic, $queue)); | ||
|
||
$consumer = $context->createConsumer($queue); | ||
$consumer->addFlag(AmqpConsumer::FLAG_NOACK); | ||
|
||
echo ' [*] Waiting for logs. To exit press CTRL+C', "\n"; | ||
|
||
while (true) { | ||
if ($message = $consumer->receive()) { | ||
echo ' [x] ', $message->getBody(), "\n"; | ||
} | ||
} | ||
|
||
$context->close(); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
// composer require enqueue/amqp-bunny | ||
require_once __DIR__.'/vendor/autoload.php'; | ||
|
||
use Enqueue\AmqpBunny\AmqpConnectionFactory; | ||
use Interop\Amqp\AmqpConsumer; | ||
use Interop\Amqp\AmqpTopic; | ||
use Interop\Amqp\Impl\AmqpBind; | ||
|
||
$config = [ | ||
'host' => 'localhost', | ||
'port' => 5672, | ||
'user' => 'guest', | ||
'pass' => 'guest', | ||
'receive_method' => 'basic_consume', | ||
]; | ||
|
||
$connection = new AmqpConnectionFactory($config); | ||
$context = $connection->createContext(); | ||
|
||
$topic = $context->createTopic('direct_logs'); | ||
$topic->setType(AmqpTopic::TYPE_DIRECT); | ||
|
||
$context->declareTopic($topic); | ||
|
||
$queue = $context->createTemporaryQueue(); | ||
|
||
$severities = array_slice($argv, 1); | ||
if (empty($severities)) { | ||
file_put_contents('php://stderr', "Usage: $argv[0] [info] [warning] [error]\n"); | ||
exit(1); | ||
} | ||
|
||
foreach ($severities as $severity) { | ||
$context->bind(new AmqpBind($topic, $queue, $severity)); | ||
} | ||
|
||
$consumer = $context->createConsumer($queue); | ||
$consumer->addFlag(AmqpConsumer::FLAG_NOACK); | ||
|
||
echo ' [*] Waiting for logs. To exit press CTRL+C', "\n"; | ||
|
||
while (true) { | ||
if ($message = $consumer->receive()) { | ||
echo ' [x] '.$message->getRoutingKey().':'.$message->getBody()."\n"; | ||
} | ||
} | ||
|
||
$context->close(); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
// composer require enqueue/amqp-bunny | ||
require_once __DIR__.'/vendor/autoload.php'; | ||
|
||
use Enqueue\AmqpBunny\AmqpConnectionFactory; | ||
use Interop\Amqp\AmqpConsumer; | ||
use Interop\Amqp\AmqpTopic; | ||
use Interop\Amqp\Impl\AmqpBind; | ||
|
||
$config = [ | ||
'host' => 'localhost', | ||
'port' => 5672, | ||
'user' => 'guest', | ||
'pass' => 'guest', | ||
'receive_method' => 'basic_consume', | ||
]; | ||
|
||
$connection = new AmqpConnectionFactory($config); | ||
$context = $connection->createContext(); | ||
|
||
$topic = $context->createTopic('topic_logs'); | ||
$topic->setType(AmqpTopic::TYPE_TOPIC); | ||
|
||
$context->declareTopic($topic); | ||
|
||
$queue = $context->createTemporaryQueue(); | ||
|
||
$binding_keys = array_slice($argv, 1); | ||
if (empty($binding_keys)) { | ||
file_put_contents('php://stderr', "Usage: $argv[0] [binding_key]\n"); | ||
exit(1); | ||
} | ||
|
||
foreach ($binding_keys as $binding_key) { | ||
$context->bind(new AmqpBind($topic, $queue, $binding_key)); | ||
} | ||
|
||
$consumer = $context->createConsumer($queue); | ||
$consumer->addFlag(AmqpConsumer::FLAG_NOACK); | ||
|
||
echo ' [*] Waiting for logs. To exit press CTRL+C', "\n"; | ||
|
||
while (true) { | ||
if ($message = $consumer->receive()) { | ||
echo ' [x] '.$message->getRoutingKey().':'.$message->getBody()."\n"; | ||
} | ||
} | ||
|
||
$context->close(); |
Oops, something went wrong.