I’ve searched and I’ve read the posts and I’m close, as I have variable calls working but having difficulty getting a function call to work. I get the feeling it’s just some simple syntax I have wrong but do not have the expertise to figure it out and now basically rereading existing posts and going around in circles.
It was all working with direct calls, but using the PHP to hide and insert access token fails on function calls.
Any assistance would be appreciated.
Working Html for variable call:
function getVariable(variable, callback) {
var url = baseURL + "?method=" + variable + "&device=" + coreID;
$.ajax({
url: url,
dataType: "json"
}).success(function(obj) {
console.log(obj);
(obj.coreInfo.deviceID && obj.coreInfo.deviceID == coreID) ? onMethodSuccess() : onMethodFailure ((obj.error)?obj.error:"");
callback(obj.result);
}).fail(function(obj) {
onMethodFailure();
});
}
Non working Html for function call:
function doMethod(vmethod, data) {
// var url = baseURL + coreID + "/" + vmethod;
var url = baseURL + "?method=" + vmethod + "&device=" + coreID ;
$.ajax({
type: "POST",
url: url,
data: {
args: data
},
dataType: "json"
}).success(function(obj) {
console.log(obj);
(obj.return_value && obj.return_value == 200) ? onMethodSuccess() : onMethodFailure((obj.error)?obj.error:"");
}).fail(function(obj) {
onMethodFailure();
});
}
Old html working without php:
// The base level run method command -----------------------------function - doMethod
function doMethod(method, data) {
var url = baseURL + coreID + "/" + method;
$.ajax({
type: "POST",
url: url,
data: {
access_token: accessToken,
args: data
},
dataType: "json"
}).success(function(obj) {
console.log(obj);
(obj.return_value && obj.return_value == 200) ? onMethodSuccess() : onMethodFailure((obj.error)?obj.error:"");
}).fail(function(obj) {
onMethodFailure();
});
}
PHP file in root directory:
<?php
$device = $_REQUEST["device"];
$method = $_REQUEST["method"];
if(!$device||!$method) die("device or method param missing\n");
// add access token
$_REQUEST["access_token"] = "123456789";
// create params
$params = http_build_query($_REQUEST);
// make request
$url = "https://api.spark.io/v1/devices/" . $device . "/" . $method . "?" . $params;
$response = file_get_contents($url);
echo $response;
?>