Created
June 8, 2018 03:29
-
-
Save wakasann/0020d31477cd7580b8f757d6cf10b783 to your computer and use it in GitHub Desktop.
use php-amqplib connect to mqttserver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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