//第二步在项目根目录创建subscribe.php<?php
require('../application/admin/controller/phpMQTT.php');$server='localhost';// change if necessary$port=1883;// change if necessary$username='admin';// set your username$password='admin888';// set your password$client_id='聪';// make sure this is unique for connecting to sever - you could use uniqid()$mqtt=newapp\admin\controller\phpMQTT($server,$port,$client_id);if(!$mqtt->connect(true,NULL,$username,$password)){exit(1);}$mqtt->debug=true;//接受主题所有$topics['hometopic/#']=array('qos'=>0,'function'=>'procMsg');$mqtt->subscribe($topics,0);while($mqtt->proc()){}$mqtt->close();functionprocMsg($topic,$msg){$domain='http://think.admin.cn/api/index/add';var_dump($msg);var_dump($topic);$data=json_decode($msg,true);var_dump($data);$arr=curl_post($domain,$data);var_dump($arr);}functioncurl_post($url,$data){$data=json_encode($data,320);// return $data;$headerArray=array("Content-type:application/json;charset='utf-8'","Accept:application/json");$curl=curl_init();curl_setopt($curl,CURLOPT_URL,$url);curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,FALSE);curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,FALSE);curl_setopt($curl,CURLOPT_POST,1);curl_setopt($curl,CURLOPT_POSTFIELDS,$data);curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);$output=curl_exec($curl);curl_close($curl);returnjson_decode($output,true);}// 在根目录public下面运行改文件 php subscribe.php statr// 在线上 nohup php subscribe.php &