简体   繁体   English

MQTT使用PHP订阅IBM Bluemix

[英]MQTT Subscribe with PHP to IBM Bluemix

I want to connect to IBM Bluemix through the MQTT protocol using PHP to subscribe to messages come from IoT Foundation. 我想使用PHP通过MQTT协议连接到IBM Bluemix,以订阅来自IoT Foundation的消息。 I use this code: 我使用以下代码:

<?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();
} 

$topics['iot-2/type/+/id/phpmqtt/evt/+/fmt/json'] = 
  array("qos"=>0, "function"=>"procmsg");
$mqtt->subscribe($topics, 0);

// process messages
while ($mqtt->proc(true)) { 

}
// disconnect
$mqtt->close();
function procmsg($topic, $msg) {
 echo "Msg Recieved: $msg";
}

?>

But the browser show this message: 但是浏览器显示以下消息:

Fatal error: Maximum execution time of 30 seconds exceeded in /Library/WebServer/Documents/phpMQTT/phpMQTT.php on line 167 致命错误:在第167行的/Library/WebServer/Documents/phpMQTT/phpMQTT.php中,超过30秒的最大执行时间

subscribe is not meant to run in the web browser as it has an infinite look, its best being run from the command line. 预订无意在Web浏览器中运行,因为它具有无限的外观,最好在命令行中运行。

If you are using the subscribe method to receive messages you can look at persistent msgs and breaking out of the loop on msg receipt. 如果您使用subscription方法接收消息,则可以查看持久消息,并在接收消息时跳出循环。

There is an example of how to use phpMQTT in the web browser in the file web-app.php of this respository https://github.com/vvaswani/bluemix-iotf-device-tracker 此存储库的文件web-app.php https://github.com/vvaswani/bluemix-iotf-device-tracker文件中提供了一个如何在Web浏览器中使用phpMQTT的示例。

You don't provide very much information about what you want to achieve by doing this; 您不会提供有关要实现的目标的太多信息。 do you want to keep sending messages to the browser until the page is closed in the browser? 您是否要一直向浏览器发送消息,直到在浏览器中关闭页面?

Server Sent Events or Websockets might be a better bet, and PHP might not be the best choice for this, because it uses up quite a lot of memory per connection (compared to node.js for example). 服务器发送事件或Websocket可能是更好的选择,PHP可能不是最佳选择,因为它每个连接占用大量内存(例如,与node.js相比)。

However if you just want to remove the 30 second PHP timeout, then you can use this function: http://php.net/manual/en/function.set-time-limit.php 但是,如果您只想删除30秒的PHP超时,则可以使用以下函数: http : //php.net/manual/en/function.set-time-limit.php

Or set max_execution_time in php.ini: http://php.net/manual/en/info.configuration.php 或在php.ini中设置max_execution_time: http//php.net/manual/en/info.configuration.php

Setting the maximum execution time to 0 should stop it from timing out. 将最大执行时间设置为0将阻止它超时。

But be warned that PHP and/or your webserver will have a limited number of concurrent HTTP connections. 但请注意,PHP和/或您的Web服务器将具有有限数量的并发HTTP连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM