我想使用amqp协议将消息从云发送到设备iot集线器,为此我正在将Rabbitmq消息代理与php中的reference一起使用。我在localhost上尝试了以下代码:
//send.php
<?php
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
$connection = new AMQPStreamConnection('HUBNAME.azure-devices.net', 5671
, 'HUBNAME.azure-devices.net/DEVICENAME/?api-version=2018-06-30'
, 'SharedAccessSignature sr=HUBNAME.azure-devices.net
&sig=XXXX&se=XXXX&skn=iothubowner');
$channel = $connection->channel();
$channel->queue_declare('hello', false, false, false, false);
$json = '{"id":123, "value":"xyz"}';
$msg = new AMQPMessage($json);
$channel->basic_publish($msg, '', 'hello');
echo " [x] Sent 'Hello World!'\n";
$channel->close();
$connection->close();
我在运行此send.php
文件时遇到错误:
严重错误:未捕获 PhpAmqpLib \ Exception \ AMQPConnectionClosedException:管道损坏或 封闭连接 C:\ xampp \ htdocs \ amqp \ vendor \ php-amqplib \ php-amqplib \ PhpAmqpLib \ Wire \ IO \ StreamIO.php:222
请帮帮我。