-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.php
45 lines (38 loc) · 1.42 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
require('vendor/autoload.php');
define('AMQP_DEBUG', false);
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Connection\AMQPSSLConnection;
use PhpAmqpLib\Message\AMQPMessage;
$url_str = getenv('CLOUDAMQP_URL')
or exit("CLOUDAMQP_URL not set");
$url = parse_url($url_str);
$vhost = ($url['path'] == '/' || !isset($url['path'])) ? '/' : substr($url['path'], 1);
$port = $url['port'];
if($url['scheme'] === "amqps") {
$port = isset($port) ? $port : 5671;
$ssl_opts = array(
'capath' => '/etc/ssl/certs'
);
$conn = new AMQPSSLConnection($url['host'], $port, $url['user'], $url['pass'], $vhost, $ssl_opts);
} else {
$port = isset($port) ? $port : 5672;
$conn = new AMQPStreamConnection($url['host'], $port, $url['user'], $url['pass'], $vhost);
}
$ch = $conn->channel();
$exchange = 'amq.direct';
$queue = 'basic_get_queue';
$ch->queue_declare($queue, false, true, false, false);
$ch->exchange_declare($exchange, 'direct', true, true, false);
$ch->queue_bind($queue, $exchange);
$msg_body = 'the body';
$msg = new AMQPMessage($msg_body, array('content_type' => 'text/plain',
'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT));
echo "Sending message...\n";
$ch->basic_publish($msg, $exchange);
$retrived_msg = $ch->basic_get($queue);
echo sprintf("Message recieved: %s\n", $retrived_msg->body);
$ch->basic_ack($retrived_msg->delivery_info['delivery_tag']);
$ch->close();
$conn->close();
?>