LavinMQ with PHP

To access LavinMQ (or any AMQP server) from PHP, php-amqplib is a good choice. ## Code example using php-amqplib

The following code snippet show how to connect, publish and consume a message.

<?php
require('vendor/autoload.php');
define('AMQP_DEBUG', false);
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Connection\AMQPSSLConnection;
use PhpAmqpLib\Message\AMQPMessage;

$url_str = getenv('HOST_URL')
  or exit("HOST_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();
?>

The full code example can be found at here.

Alternative clients

#### PECL AMQP PECL AMQP library built on top of the RabbitMQ C client

Ready to take the next steps? Here are some things you should keep in mind:

Managed LavinMQ instance on CloudAMQP

LavinMQ has been built with performance and ease of use in mind - we've benchmarked a throughput of about 1,000,000 messages/sec. You can try LavinMQ without any installation hassle by creating a free instance on CloudAMQP. Signing up is a breeze.

Help and feedback

We welcome your feedback and are eager to address any questions you may have about this piece or using LavinMQ. Join our Slack channel to connect with us directly. You can also find LavinMQ on GitHub.