{"id":7768,"date":"2020-12-21T11:48:26","date_gmt":"2020-12-21T03:48:26","guid":{"rendered":"https:\/\/blog.hoyo.idv.tw\/?p=7768"},"modified":"2022-06-07T10:19:48","modified_gmt":"2022-06-07T02:19:48","slug":"php-mqtt","status":"publish","type":"post","link":"https:\/\/blog.hoyo.idv.tw\/?p=7768","title":{"rendered":"PHP - \u4f7f\u7528 WebSocket \u9023\u63a5 EMQX MQTT Broker"},"content":{"rendered":"<p>--<\/p>\n<h2>\u53c3\u8003\u8cc7\u6e90<\/h2>\n<ul>\n<li><a class=\"\" href=\"https:\/\/github.com\/bluerhinos\/phpMQTT\" target=\"_blank\" rel=\"noopener\" data-pjax=\"#js-repo-pjax-container\">phpMQTT<\/a><\/li>\n<\/ul>\n<p>\u9664\u4e86\u4e0a\u8ff0\u8cc7\u6e90\u5916\uff0c\u4e00\u958b\u59cb\u5176\u5be6\u4f7f\u7528\u7684\u662f\u00a0<a class=\"\" href=\"https:\/\/github.com\/php-mqtt\/client\" target=\"_blank\" rel=\"noopener\" data-pjax=\"#js-repo-pjax-container\">php-mqtt\/client<\/a>\u00a0\u9019\u500b\uff0c\u4e0d\u904e\u8edf\u9ad4\u592a\u80a5\uff0c\u4f7f\u7528\u9ebb\u7169\u6548\u80fd\u4e5f\u5dee\uff0c\u624d\u53c8\u627e\u4e86 phpMQTT \u4f7f\u7528<\/p>\n<p>--<\/p>\n<h2>\u7bc4\u4f8b\u7a0b\u5f0f<\/h2>\n<p>\u4e3b\u8981\u662f\u5728\u7db2\u9801\u4e0a\u57f7\u884c publish() \u767c\u9001\uff0c\u56e0\u6b64\u53ea\u9700\u8981\u53ef\u9023\u7dda\uff0c\u53ef\u767c\u9001\u5373\u53ef<\/p>\n<pre class=\"lang:php decode:true\">&lt;?php\r\n\r\nrequire('..\/3rdParty\/phpMQTT.php');\r\n\r\n$server = 'mq.tongxinmao.com';     \/\/ change if necessary\r\n$port = 18830;                     \/\/ change if necessary\r\n$username = '';                   \/\/ set your username\r\n$password = '';                   \/\/ set your password\r\n$client_id = 'phpMQTT-publisher'; \/\/ make sure this is unique for connecting to sever - you could use uniqid()\r\n\r\n$mqtt = new \\phpMQTT($server, $port, $client_id);\r\n\r\nif ($mqtt-&gt;connect(true, NULL)) {\r\n    $mqtt-&gt;publish('esp8266\/test', '{\"LED\":\"'. $_GET['led'] .'\"}', 0, false);\r\n    $mqtt-&gt;close();\r\n} else {\r\n    echo \"Time out!\\n\";\r\n}<\/pre>\n<p>--<\/p>\n<h2>SSL \u7bc4\u4f8b<\/h2>\n<pre class=\"lang:php decode:true \">&lt;?php\r\n\/\/ MQTT\r\nconst MQTT_Server = '172.16.1.220';\r\nconst MQTT_Port = 8883;\r\nconst MQTT_Username = 'user';\r\nconst MQTT_Password = 'password';\r\n\r\nrequire_once __DIR__ . \"\/phpMQTT.php\";\r\n$mqtt = new \\phpMQTT(MQTT_Server, MQTT_Port, 'EMS_'.uniqid(), __DIR__.'\/my_root_ca.pem');\r\n\r\nif ($mqtt-&gt;connect(true, NULL, MQTT_Username, MQTT_Password)) {\r\n    $alert = [\r\n        'id'=&gt;$event_id,\r\n        'event'=&gt;'PM2.5',\r\n        'time'=&gt;date('Y-m-d H:i:s'),\r\n        'period'=&gt;'start',\r\n        'set'=&gt;$pm25_alarm_up,\r\n        'value'=&gt;$data['Value']['PM2.5']\r\n    ];\r\n    $mqtt-&gt;publish('DAE\/Alert\/'.$channel['Token'], json_encode($alert), 0, false);\r\n    $mqtt-&gt;close();\r\n}<\/pre>\n<p>--<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"7768\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;2,321&nbsp;total views, &nbsp;1&nbsp;views today<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>-- \u53c3\u8003\u8cc7\u6e90 phpMQTT...<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"7768\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;2,321&nbsp;total views, &nbsp;1&nbsp;views today<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[31],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/7768"}],"collection":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7768"}],"version-history":[{"count":7,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/7768\/revisions"}],"predecessor-version":[{"id":10174,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/7768\/revisions\/10174"}],"wp:attachment":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7768"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7768"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7768"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}