Ok, still confused here, not necessarily with what is going on here, but how one does it. It took me a while to look at the javascript and php to see what was happening, but I think I got it. I’m writing some test code:
<!DOCTYPE HTML>
<html>
<head>
<title>test</title>
<style>
body { margin: 0; }
canvas { width: 100%; height: 100% }
</style>
<meta charset="utf-8">
</head>
<body>
<div align = "center" id="div">DIV</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script>
var CORE_ID = '########################';
test();
function test(){
var div = document.getElementById("div");
div.innerHTML = "MAKING REQUEST";
$.get('/proxy.php?'+CORE_ID+'/variable_name', function(response) {
console.log(response);
div.innerHTML=response;
});
}
</script>
</body>
</html>
So from the requesting HTML page, an ajax request is made to proxy.php using the GET method, and in this request, the core ID is sent along with what resource. It used to be a variable name, but I put events in there because I want to subscribe.
Here’s wgbartley’s PHP proxy script I copied from his gist here:
https://gist.github.com/wgbartley/11337650
<?
// Set your access token here
define('ACCESS_TOKEN', 'your_access_token_here');
// All responses should be JSON
header('Content-type: application/json');
// Build the URL. Since it's possible to accidentally have an
// extra / or two in $_SERVER['QUERY_STRING], replace "//" with "/"
// using str_replace(). This also appends the access token to the URL.
$url = 'https://'.str_replace('//', '/', 'api.spark.io/v1/devices/'.$_SERVER['QUERY_STRING'].'?access_token='.ACCESS_TOKEN);
// HTTP GET requests are easy!
if(strtoupper($_SERVER['REQUEST_METHOD'])=='GET')
echo file_get_contents($url);
// HTTP POST requires the use of cURL
elseif (strtoupper($_SERVER['REQUEST_METHOD'])=='POST') {
$c = curl_init();
curl_setopt_array($c, array(
// Set the URL to access
CURLOPT_URL => $url,
// Tell cURL it's an HTTP POST request
CURLOPT_POST => TRUE,
// Include the POST data
// $HTTP_RAW_POST_DATA may work on some servers, but it's deprecated in favor of php://input
CURLOPT_POSTFIELDS => file_get_contents('php://input'),
// Return the output to a variable instead of automagically echoing it (probably a little redundant)
CURLOPT_RETURNTRANSFER => TRUE
));
// Make the cURL call and echo the response
echo curl_exec($c);
// Close the cURL resource
curl_close($c);
}
?>
What I was mistaken about is what happens next. I now (correctly?) think that PHP takes the information in the GET method and puts it into the $url variable in the right spot so you have a well formed URL for requesting a variable from a spark core with the access token also put in.
Now here’s where my greatest amount of confusion lies: In the subscribe tutorial, a URL is created in the HTML page and is passed to the page’s eventSource() constructor. If I don’t want the access token to be in the HTML file, use a proxy, but the proxy does not return the URL, it returns the contents of that URL. If I were looking for a spark variable, the value is what would be echoed back, not it’s location. So I don’t have a URL to give to the eventSource() constructor to subscribe to.