Skip to content

Commit f4c2d12

Browse files
ramunasdramunasdronga
authored andcommitted
fix: improve php library tutorial
1 parent 2799c1e commit f4c2d12

9 files changed

+41
-35
lines changed

php/README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ To successfully use the examples you will need a running RabbitMQ server.
77

88
## Requirements
99

10-
### PHP 7.0+
10+
### PHP 7.2+
1111

12-
You need `PHP 7.0` and `php-amqplib`. To get these
12+
You need `PHP 7.2` and `php-amqplib`. To get these
1313
dependencies on Ubuntu type:
1414

15-
sudo apt-get install git-core php5-cli
15+
sudo apt-get install git-core php-cli
1616

1717

1818
### Composer
@@ -27,11 +27,11 @@ Then you can install `php-amqplib` using [Composer](https://getcomposer.org).
2727
To do that install Composer and add it to your path, then run the following command
2828
inside this project folder:
2929

30-
composer.phar install
30+
php composer.phar install
3131

3232
Or you can require it to the existing project using a command:
3333

34-
composer.phar require php-amqplib/php-amqplib
34+
php composer.phar require php-amqplib/php-amqplib
3535

3636
## Code
3737

php/composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"require": {
3-
"php-amqplib/php-amqplib": ">=3.0"
3+
"php-amqplib/php-amqplib": "^3.2"
44
}
55
}

php/receive.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@
1111
echo " [*] Waiting for messages. To exit press CTRL+C\n";
1212

1313
$callback = function ($msg) {
14-
echo ' [x] Received ', $msg->body, "\n";
14+
echo ' [x] Received ', $msg->getBody(), "\n";
1515
};
1616

1717
$channel->basic_consume('hello', '', false, true, false, false, $callback);
1818

19-
while ($channel->is_open()) {
20-
$channel->wait();
19+
try {
20+
$channel->consume();
21+
} catch (\Throwable $exception) {
22+
echo $exception->getMessage();
2123
}
2224

2325
$channel->close();
2426
$connection->close();
25-
?>

php/receive_logs.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@
1515
echo " [*] Waiting for logs. To exit press CTRL+C\n";
1616

1717
$callback = function ($msg) {
18-
echo ' [x] ', $msg->body, "\n";
18+
echo ' [x] ', $msg->getBody(), "\n";
1919
};
2020

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

23-
while ($channel->is_open()) {
24-
$channel->wait();
23+
try {
24+
$channel->consume();
25+
} catch (\Throwable $exception) {
26+
echo $exception->getMessage();
2527
}
2628

2729
$channel->close();
2830
$connection->close();
29-
?>

php/receive_logs_direct.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@
2323
echo " [*] Waiting for logs. To exit press CTRL+C\n";
2424

2525
$callback = function ($msg) {
26-
echo ' [x] ', $msg->delivery_info['routing_key'], ':', $msg->body, "\n";
26+
echo ' [x] ', $msg->getRoutingKey(), ':', $msg->getBody(), "\n";
2727
};
2828

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

31-
while ($channel->is_open()) {
32-
$channel->wait();
31+
try {
32+
$channel->consume();
33+
} catch (\Throwable $exception) {
34+
echo $exception->getMessage();
3335
}
3436

3537
$channel->close();
3638
$connection->close();
37-
?>

php/receive_logs_topic.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@
2323
echo " [*] Waiting for logs. To exit press CTRL+C\n";
2424

2525
$callback = function ($msg) {
26-
echo ' [x] ', $msg->delivery_info['routing_key'], ':', $msg->body, "\n";
26+
echo ' [x] ', $msg->getRoutingKey(), ':', $msg->getBody(), "\n";
2727
};
2828

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

31-
while ($channel->is_open()) {
32-
$channel->wait();
31+
try {
32+
$channel->consume();
33+
} catch (\Throwable $exception) {
34+
echo $exception->getMessage();
3335
}
3436

3537
$channel->close();
3638
$connection->close();
37-
?>

php/rpc_client.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function __construct()
4545
public function onResponse($rep)
4646
{
4747
if ($rep->get('correlation_id') == $this->corr_id) {
48-
$this->response = $rep->body;
48+
$this->response = $rep->getBody();
4949
}
5050
}
5151

php/rpc_server.php

+7-6
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,30 @@ function fib($n)
2222

2323
echo " [x] Awaiting RPC requests\n";
2424
$callback = function ($req) {
25-
$n = intval($req->body);
25+
$n = intval($req->getBody());
2626
echo ' [.] fib(', $n, ")\n";
2727

2828
$msg = new AMQPMessage(
2929
(string) fib($n),
3030
array('correlation_id' => $req->get('correlation_id'))
3131
);
3232

33-
$req->delivery_info['channel']->basic_publish(
33+
$req->getChannel()->basic_publish(
3434
$msg,
3535
'',
3636
$req->get('reply_to')
3737
);
3838
$req->ack();
3939
};
4040

41-
$channel->basic_qos(null, 1, null);
41+
$channel->basic_qos(null, 1, false);
4242
$channel->basic_consume('rpc_queue', '', false, false, false, false, $callback);
4343

44-
while ($channel->is_open()) {
45-
$channel->wait();
44+
try {
45+
$channel->consume();
46+
} catch (\Throwable $exception) {
47+
echo $exception->getMessage();
4648
}
4749

4850
$channel->close();
4951
$connection->close();
50-
?>

php/worker.php

+7-6
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,20 @@
1111
echo " [*] Waiting for messages. To exit press CTRL+C\n";
1212

1313
$callback = function ($msg) {
14-
echo ' [x] Received ', $msg->body, "\n";
15-
sleep(substr_count($msg->body, '.'));
14+
echo ' [x] Received ', $msg->getBody(), "\n";
15+
sleep(substr_count($msg->getBody(), '.'));
1616
echo " [x] Done\n";
1717
$msg->ack();
1818
};
1919

20-
$channel->basic_qos(null, 1, null);
20+
$channel->basic_qos(null, 1, false);
2121
$channel->basic_consume('task_queue', '', false, false, false, false, $callback);
2222

23-
while ($channel->is_open()) {
24-
$channel->wait();
23+
try {
24+
$channel->consume();
25+
} catch (\Throwable $exception) {
26+
echo $exception->getMessage();
2527
}
2628

2729
$channel->close();
2830
$connection->close();
29-
?>

0 commit comments

Comments
 (0)