-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproxy.php
More file actions
61 lines (51 loc) · 1.53 KB
/
proxy.php
File metadata and controls
61 lines (51 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
/**
* Author: Trimbitas Sorin
* Email: trimbitassorin@hotmail.com
*/
// Validate parameters
if ( ! isset($_GET['topic']) OR ! isset($_GET['msg']))
{
die('You need to set "topic" and "msg" as GET parameters!');
}
$topic = trim($_GET['topic']);
$msg = trim($_GET['msg']);
if ( ! $topic)
{
die('Invalid topic!');
}
// All is ok .. load the library and initialize it
require_once(__DIR__.'/lib/mqtt.lib.php');
$config = [
'aws_iot_host' => 'A323VJS9G76W0W.iot.eu-west-1.amazonaws.com',
'aws_iot_port' => 8883,
'aws_iot_cafile' => __DIR__.'/certs/VeriSign-Class-3-Public-Primary-Certification-Authority-G5.pem',
'aws_iot_crtfile' => __DIR__.'/certs/7e54b41b6a-certificate.pem.crt',
'aws_iot_private_keyfile' => __DIR__.'/certs/7e54b41b6a-private.pem.key',
];
// Init MQTT Library
$mqtt = new libMQTT\client($config['aws_iot_host'], $config['aws_iot_port'], 'php_proxy');
$mqtt->setClientCert($config['aws_iot_crtfile'], $config['aws_iot_private_keyfile']);
$mqtt->setCAFile($config['aws_iot_cafile']);
$mqtt->setCryptoProtocol('ssl');
$mqtt->setVerbose(1);
// Try to connect
if ( ! $mqtt->connect())
{
die('0'.'');
}
// We are connected :)
// Publish the GET parameters
// Third parameter is QOS = Quality of Service, check this for more information http://docs.aws.amazon.com/iot/latest/developerguide/protocols.html
$reply = $mqtt->publish($topic, $msg, 0);
// Close the connection
$mqtt->close();
if ($reply)
{
// Publish action succeded! :)
echo 1;
die();
}
// Sadly, publish action failed :(
echo 0;
die();