Skip to content

Instantly share code, notes, and snippets.

@wakasann
Created June 8, 2018 03:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wakasann/0020d31477cd7580b8f757d6cf10b783 to your computer and use it in GitHub Desktop.
Save wakasann/0020d31477cd7580b8f757d6cf10b783 to your computer and use it in GitHub Desktop.
use php-amqplib connect to mqttserver
<?php
//php connection to mqtt server(rabbitmq)
//use https://packagist.org/packages/php-amqplib/php-amqplib
define('AMQP_DEBUG', true);
require_once __DIR__.'/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
$exchange = 'fanout2';
$queue = 'test';
$connection = new AMQPStreamConnection("192.168.1.22",5672, 'guest2', 'guest2','/');
$channel = $connection->channel();
/*
name: $queue
passive: false
durable: true // the queue will survive server restarts
exclusive: false // the queue can be accessed in other channels
auto_delete: false //the queue won't be deleted once the channel is closed.
*/
$channel->queue_declare($queue, false, true, false, true);
/*
name: $exchange
type: direct
passive: false
durable: true // the exchange will survive server restarts
auto_delete: false //the exchange won't be deleted once the channel is closed.
//topic
//direct
*/
$channel->exchange_declare($exchange, 'topic', false, true, false);
$channel->queue_bind($queue, $exchange,'test');
$msg = new AMQPMessage('nothing');
$channel->basic_publish($msg,$exchange,'test');
$channel->close();
$connection->close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment