Skip to content

Instantly share code, notes, and snippets.

@developersteve
Created September 17, 2018 22:44
Show Gist options
  • Save developersteve/c56b215f220187baeedd2a9ac8a340fa to your computer and use it in GitHub Desktop.
Save developersteve/c56b215f220187baeedd2a9ac8a340fa to your computer and use it in GitHub Desktop.
SMS broker callback
<?php
require_once(__DIR__ . '/vendor/autoload.php');
require_once(__DIR__ . '/inc.php');
require("phpMQTT.php");
$server = "iot.broker.com";
$port = 1883;
$username = "";
$password = "";
$mqtt_id = "server-publisher";
$client_id = CLIENT_ID; // string |
$client_secret = CLIENT_SECRET; // string |
$grant_type = GRANT_TYPE; // string |
$apiInstance = new Telstra_Messaging\Api\AuthenticationApi(
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
// This is optional, `GuzzleHttp\Client` will be used as default.
new GuzzleHttp\Client()
);
$result = $apiInstance->authToken($client_id, $client_secret, $grant_type);
$config = Telstra_Messaging\Configuration::getDefaultConfiguration()->setAccessToken($result['access_token']);
$apiInstance = new Telstra_Messaging\Api\MessagingApi(
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
// This is optional, `GuzzleHttp\Client` will be used as default.
new GuzzleHttp\Client(),
$config
);
$mqtt = new phpMQTT($server, $port, $mqtt_id);
$data = file_get_contents('php://input');
$data = json_decode($data, true);
if($data['messageId']){
$arr = explode(' ',trim($data['body']));
if(strtolower($arr[0])=="lights"){
if ($mqtt->connect(true, NULL, $username, $password)) {
$mqtt->publish("lights", strtolower($arr[1]), 0);
$mqtt->close();
} else {
echo "Time out!\n";
}
}
elseif(strtolower($arr[0])=="safe"){
if ($mqtt->connect(true, NULL, $username, $password)) {
$mqtt->publish("safe", strtolower($arr[1]), 0);
$mqtt->close();
} else {
echo "Time out!\n";
}
}
}
if(isset($data['from'])){
$arr = array('to' => $data['from'], 'body' => $arr[1].' added to the iot broker for '.$arr[0]);
$payload = new \Telstra_Messaging\Model\SendSMSRequest($arr);
$result = $apiInstance->sendSMS($payload);
print_r($result);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment