Skip to content

Commit d11638a

Browse files
committed
example 1 basic send receive
1 parent 5007bfb commit d11638a

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

php-amqp/receive.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env php
2+
<?php
3+
/**
4+
@author Chimdi Azubuike <me@chimdi.com>
5+
*/
6+
7+
//Establish connection AMQP
8+
$connection = new AMQPConnection();
9+
$connection->setHost('127.0.0.1');
10+
$connection->setLogin('guest');
11+
$connection->setPassword('guest');
12+
$connection->connect();
13+
14+
//Create and declare channel
15+
$channel = new AMQPChannel($connection);
16+
17+
//AMQPC Exchange is the publishing mechanism
18+
$exchange = new AMQPExchange($channel);
19+
20+
$routing_key = 'hello';
21+
$queue = new AMQPQueue($channel);
22+
$queue->setName($routing_key);
23+
$queue->setFlags(AMQP_NOPARAM);
24+
$queue->declareQueue();
25+
26+
27+
echo ' [*] Waiting for messages. To exit press CTRL+C ', PHP_EOL;
28+
29+
$callback_func = function(AMQPEnvelope $message, AMQPQueue $q) use (&$max_consume) {
30+
echo PHP_EOL, "------------", PHP_EOL;
31+
echo " [x] Received ", $message->getBody(), PHP_EOL;
32+
echo PHP_EOL, "------------", PHP_EOL;
33+
34+
$q->nack($message->getDeliveryTag());
35+
sleep(1);
36+
};
37+
38+
try{
39+
$queue->consume($callback_func);
40+
}catch(AMQPQueueException $ex){
41+
print_r($ex);
42+
}catch(Exception $ex){
43+
print_r($ex);
44+
}
45+
46+
echo 'Close connection...', PHP_EOL;
47+
$queue->cancel();
48+
$connection->disconnect();
49+

php-amqp/send.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env php
2+
<?php
3+
/**
4+
@author Chimdi Azubuike <me@chimdi.com>
5+
*/
6+
7+
//Establish connection to AMQP
8+
$connection = new AMQPConnection();
9+
$connection->setHost('127.0.0.1');
10+
$connection->setLogin('guest');
11+
$connection->setPassword('guest');
12+
$connection->connect();
13+
14+
//Create and declare channel
15+
$channel = new AMQPChannel($connection);
16+
17+
18+
//AMQPC Exchange is the publishing mechanism
19+
$exchange = new AMQPExchange($channel);
20+
21+
22+
try{
23+
$routing_key = 'hello';
24+
25+
$queue = new AMQPQueue($channel);
26+
$queue->setName($routing_key);
27+
$queue->setFlags(AMQP_NOPARAM);
28+
$queue->declareQueue();
29+
30+
31+
$message = 'howdy-do';
32+
$exchange->publish($message, $routing_key);
33+
}catch(Exception $ex){
34+
print_r($ex);
35+
}

0 commit comments

Comments
 (0)