This class connects to the Flukso API url and authenticates with your sensorid and token. Notice that the class uses the cURL library to make the HTTP request, so make sure this extension is enabled in your PHP configuration (thanks Rene for pointing this out). The sensorid and token in the example are from the sensor of Flukso Bart.
You can download a copy of the Flukso API PHP class from this source. Just rename the extension to .php, get and set your Flukso settings and you are ready to go!
Good luck,
Geert
- /*
- PHP FLukso API class
- Date: 2010/04/21
- Author: Geert Van Bommel
- Licence: free as is, use it and modify it any way you want
- Note : sensorid and tokenid used in the example are Flukso Bart's
- */
- class Flukso {
- private $sensorid, $token, $interval, $unit;
- private $url='https://api.flukso.net/sensor/';
- public function __construct($u_sensorid, $u_token, $u_interval, $u_unit) {
- $this->sensorid = $u_sensorid;
- $this->token = $u_token;
- $this->interval = 'hour';
- } else {
- $this->interval = $u_interval;
- }
- $this->unit = 'watt';
- } else {
- $this->unit = $u_unit;
- }
- $this->getdata();
- }
- private function getdata() {
- /* headers according to
- <a href="http://www.flukso.net/content/jsonrest-api-released<br />
- " title="http://www.flukso.net/content/jsonrest-api-released<br />
- ">http://www.flukso.net/content/jsonrest-api-released<br />
- </a> */
- $header[]="Accept: application/json";
- $header[]="X-Version: 1.0";
- $header[]='X-Token: '.$this->token;
- $request=$this->url.$this->sensorid;
- $request.='?interval='.$this->interval.'&unit='.$this->unit;
- }
- public function __toString() {
- return $this->data;
- }
- }
- ###### usage ##############################################
- /* your Flukso settings */
- $sensorid='c1411c6b4f9910bbbab09f145f8533b9';
- /* Your sensor id's are displayed under the 'my account' -> 'sensor' tab.
- Just substitute the sensor id and token with your own and you're ready to go. */
- $token='d8a8ab8893ea73f768b66b45234b5c3a';
- /* A token associated with the specific sensor id.
- Different tokens per sensor will allow for varying access permissions,
- e.g. only allow data in 'month' resolution.
- This feature will become available in the near future.
- We now provision a single token per sensor.
- The token can be found next to its respective sensor id. */
- $interval='hour'; /* Should be one of {hour, day, month, year, night} */
- $unit='watt'; /* Should be one of {watt, kwhperyear, eurperyear, audperyear} */
- /* That's it, now get the data !! */
- $fluksodata = new Flukso($sensorid, $token, $interval, $unit);
- /* a json object is returned */
- print($fluksodata);
- /* to make it a php array, use json_decode */
- ##########################################################
Sweet! 20 karma points awarded to Geert.
Cheers,
Flukso Bart.
Great, thanks !
added resolution: