如何使用MQTT和PHP获取IOT Foundation中的设备列表。 我试过这个代码:
<?php
require("../phpMQTT.php");
$config = array(
'org_id' => 't9m318',
'port' => '1883',
'app_id' => 'phpmqtt',
'iotf_api_key' => 'my api key',
'iotf_api_secret' => 'my api secret',
'device_id' => 'phpmqtt'
);
$config['server'] = $config['org_id'] . '.messaging.internetofthings.ibmcloud.com';
$config['client_id'] = 'a:' . $config['org_id'] . ':' . $config['app_id'];
$location = array();
// initialize client
$mqtt = new phpMQTT($config['server'], $config['port'], $config['client_id']);
$mqtt->debug = false;
// connect to broker
if(!$mqtt->connect(true, null, $config['iotf_api_key'], $config['iotf_api_secret'])){
echo 'ERROR: Could not connect to IoT cloud';
exit();
}
但我不知道应该为订阅TOPIC做什么。
然后我更改代码并尝试使用HTTP而不是MQTT:
$ UserPass = $ config [&#39; iotf_api_key&#39;]。&#39;:&#39;。$ config [&#39; iotf_api_secret&#39;];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 't9m318.messaging.internetofthings.ibmcloud.com');
curl_setopt($ch, CURLOPT_USERPWD, $UserPass);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json','Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$output = curl_exec($ch);
curl_close($ch);
$arr = json_decode($output,true);
echo $arr;
但它不起作用!
由于
答案 0 :(得分:1)
您无法通过MQTT获取设备列表,但您可以使用HTTP API列出所有设备。
您可以通过以下方式获取所有设备:
https://docs.internetofthings.ibmcloud.com/swagger/v0002.html#!/Bulk_Operations/get_bulk_devices
或按deviceType过滤: