Get variables from a MySQL database

Here is an example bit of code i very quickly whipped up, the query part is missing some bits as its a cut and paste from another PHP script, but you will get the idea. you will see right at the end there is ‘addmessage’ in the send curl function call, thats the name of the function on the core (it sends a new comment to the spark message torch so have a look at the functions in that to see the core side of things). the PHP script has a security check to make sure the right thing is calling it, and not just a robot, i trigger it with a webhook from spark at the moment, and the event name that triggers the webhook gets checked… but you could cut all that out if its not suitable.

<?php

$spark_id = 'type core ID'; //torch core
$spark_access_token = 'type access token here';

function sendCurl($spark_id, $spark_access_token, $type, $postfields) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.spark.io/v1/devices/' . $spark_id . '/' . $type);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'access_token=' . $spark_access_token . '&args=' . $postfields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$result = curl_exec($ch);
$answer = json_decode($result, true);

if ($answer['return_value']==1) {
return;
}
else {
        $file = 'connect.txt';
        file_put_contents($file, 'Curl Failed' , FILE_APPEND);
        die(); 
}
}//end of sendCurl function


if (isset($_POST['event']) && isset($_POST['coreid'])) {


    //split the event into spark user id and event name
    list($sparkuid, $event) = explode( '/', ($_POST['event']), 2); 
    if (!ctype_xdigit($sparkuid)) { 
        $file = 'connect.txt';
        file_put_contents($file, 'sparkuid not hex' , FILE_APPEND);
        die(); } //check spark user id is hex characters
    if ($event != 'messagefinished') { 
        $file = 'connect.txt';
        file_put_contents($file, 'wrong event' , FILE_APPEND);
        die();  }
    
    $coreid = $_POST['coreid'];
    if (!ctype_xdigit($coreid)) { 
        $file = 'connect.txt';
        file_put_contents($file, 'wrong core ID' , FILE_APPEND);
        die(); } //check spark core id is hex characters
    //$coreid = cmtx_sanitize($coreid, true, true);
    
    if ($coreid != $spark_id) { 
        $file = 'connect.txt';
        file_put_contents($file, 'wrong core' , FILE_APPEND);
        die();  } //must be the right core
} else { $file = 'connect.txt';
        file_put_contents($file, 'Post fields not set' , FILE_APPEND);
        die();  } //must be set

$comments = mysqli_query($this->getLink(), "SELECT * FROM `" . $this->getPrefix() . "comments` WHERE `is_approved` = '1' AND `displayed` = '0' ORDER BY `dated` ASC LIMIT 1");

$comment = mysqli_fetch_array($comments);

$message = $comment['comment'];

sendCurl($spark_id, $spark_access_token, 'addmessage' , (urlencode($message)));

?>
1 Like