Flukso API PHP Class

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

  1. /*
  2. PHP FLukso API class
  3.  
  4. Date: 2010/04/21
  5. Author: Geert Van Bommel
  6. Licence: free as is, use it and modify it any way you want
  7.  
  8. Note : sensorid and tokenid used in the example are Flukso Bart's
  9. */
  10. class Flukso {
  11.         private $sensorid, $token, $interval, $unit;
  12.         private $url='https://api.flukso.net/sensor/';
  13.        
  14.         public function __construct($u_sensorid, $u_token, $u_interval, $u_unit) {
  15.                 $this->sensorid = $u_sensorid;
  16.                 $this->token = $u_token;
  17.                 if (empty($u_interval)) {
  18.                         $this->interval = 'hour';
  19.                 } else {
  20.                         $this->interval = $u_interval;
  21.                 }      
  22.                 if (empty($u_unit)) {
  23.                         $this->unit = 'watt';
  24.                 } else {
  25.                         $this->unit = $u_unit;
  26.                 }
  27.                 $this->getdata();
  28.         }
  29.  
  30.         private function getdata() {
  31.                 /* headers according to
  32.                    <a href="http://www.flukso.net/content/jsonrest-api-released<br />
  33. " title="http://www.flukso.net/content/jsonrest-api-released<br />
  34. ">http://www.flukso.net/content/jsonrest-api-released<br />
  35. </a>            */
  36.                 $header=array();
  37.                 $header[]="Accept: application/json";
  38.                 $header[]="X-Version: 1.0";
  39.                 $header[]='X-Token: '.$this->token;
  40.  
  41.                 $request=$this->url.$this->sensorid;
  42.                 $request.='?interval='.$this->interval.'&unit='.$this->unit;
  43.                
  44.                 $ch=curl_init();
  45.                 curl_setopt($ch,CURLOPT_URL,$request);
  46.                 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  47.                 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  48.                 curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  49.                 curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  50.                 $this->data=curl_exec($ch);
  51.                 curl_close($ch);
  52.                
  53.         }
  54.         public function __toString() {
  55.                 return $this->data;
  56.         }
  57.  
  58. }
  59.  
  60.  
  61. ###### usage ##############################################
  62.  
  63. /* your Flukso settings */
  64.  
  65. $sensorid='c1411c6b4f9910bbbab09f145f8533b9';
  66. /* Your sensor id's are displayed under the 'my account' -> 'sensor' tab.
  67. Just substitute the sensor id and token with your own and you're ready to go. */
  68.  
  69. $token='d8a8ab8893ea73f768b66b45234b5c3a';
  70. /* A token associated with the specific sensor id.
  71. Different tokens per sensor will allow for varying access permissions,
  72.  e.g. only allow data in 'month' resolution.
  73. This feature will become available in the near future.
  74. We now provision a single token per sensor.
  75. The token can be found next to its respective sensor id. */
  76.  
  77. $interval='hour'; /* Should be one of {hour, day, month, year, night} */
  78.  
  79. $unit='watt';   /* Should be one of {watt, kwhperyear, eurperyear, audperyear} */
  80.  
  81.  
  82. /* That's it, now get the data !! */
  83. $fluksodata = new Flukso($sensorid, $token, $interval, $unit);
  84.  
  85. /* a json object is returned */
  86. print($fluksodata);
  87.  
  88. /* to make it a php array, use json_decode */
  89. $fluksodata=json_decode($fluksodata);
  90. print_r($fluksodata);
  91.  
  92. ##########################################################

icarus75's picture

Sweet! 20 karma points awarded to Geert.

Cheers,
Flukso Bart.

dijka007's picture

Great, thanks !

added resolution:

  1. /*
  2. PHP FLukso API class
  3.  
  4. Date: 2010/04/21
  5. Author: Geert Van Bommel
  6. Licence: free as is, use it and modify it any way you want
  7.  
  8. Note : sensorid and tokenid used in the example are Flukso Bart's
  9.  
  10. 2014/04/20 added: resolution
  11.  
  12. */
  13.  
  14.  
  15. # your Flukso settings
  16.  
  17. # Your sensor id's are displayed under the 'my account' -> 'sensor' tab.
  18. # Just substitute the sensor id and token with your own and you're ready to go.
  19. $sensorid='c1411c6b4f9910bbbab09f145f8533b9';
  20. /* A token associated with the specific sensor id
  21. $token='d8a8ab8893ea73f768b66b45234b5c3a';
  22. $interval='day';        # Should be one of {hour, day, month, year, night}
  23. $unit='lpermin';        # Should be one of {watt, kwhperyear, eurperyear, audperyear, lpermin, lperday, m3peryear}
  24. $resolution='minute';   # Should be one of {minute, 15min, hour, day, week, month, year, decade, night}
  25.  
  26.  
  27. class Flukso {
  28.         private $sensorid, $token, $interval, $unit, $resolution;
  29.         private $url='https://api.flukso.net/sensor/';
  30.        
  31.         public function __construct($u_sensorid, $u_token, $u_interval, $u_unit, $u_resolution) {
  32.                 $this->sensorid = $u_sensorid;
  33.                 $this->token = $u_token;
  34.                 if (empty($u_interval)) {
  35.                         $this->interval = 'hour';
  36.                 } else {
  37.                         $this->interval = $u_interval;
  38.                 }
  39.                 if (empty($u_unit)) {
  40.                         $this->unit = 'watt';
  41.                 } else {
  42.                         $this->unit = $u_unit;
  43.                 }
  44.                 $this->resolution = $u_resolution;
  45.                 $this->getdata();
  46.         }
  47.  
  48.         private function getdata() {
  49.                 // headers according to <a href="http://www.flukso.net/content/jsonrest-api-released<br />
  50. " title="http://www.flukso.net/content/jsonrest-api-released<br />
  51. ">http://www.flukso.net/content/jsonrest-api-released<br />
  52. </a>            $header=array();
  53.                 $header[]="Accept: application/json";
  54.                 $header[]="X-Version: 1.0";
  55.                 $header[]='X-Token: '.$this->token;
  56.  
  57.                 $request=$this->url.$this->sensorid.'?interval='.$this->interval.'&unit='.$this->unit.'&resolution='.$this->resolution;
  58.  
  59.                 $ch=curl_init();
  60.                 curl_setopt($ch,CURLOPT_URL,$request);
  61.                 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  62.                 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  63.                 curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  64.                 curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  65.                 $this->data=curl_exec($ch);
  66.                 curl_close($ch);
  67.                
  68.         }
  69.         public function __toString() {
  70.                 return $this->data;
  71.         }
  72.  
  73. }
  74.  
  75.  
  76. $fluksodata = new Flukso($sensorid, $token, $interval, $unit, $resolution);
  77.  
  78. # a json object is returned
  79. #
  80. print($fluksodata);
  81.  
  82. # to make it a php array, use json_decode
  83. $fluksodata=json_decode($fluksodata);
  84. print_r($fluksodata);