Good. I have a json file that I wanted to convert to 2 string in php and then write to the database; my json currently comes like this:
[
[
1455628560,
2340
],
[
1455628620,
2340
],
]
My problem is to select the UNIX TimeStamp date and put it in a string and put the low values in another string.
So far I'm working with the following code;
<?php
$sensorid='###';
$token='###';
$interval='###'; #{hour, day, month, year, night}
$unit='###'; #{watt, kwhperyear, eurperyear, audperyear, lpermin, lperday, m3peryear}
$resolution='###'; # {minute, 15min, hour, day, week, month, year, decade, night}
$db_user = "###";
$db_pass = "###";
$db_host = "###";
$db_name = "###";
$table = "###";
class Flukso {
private $sensorid, $token, $interval, $unit, $resolution;
private $url='https://api.flukso.net/sensor/';
public function __construct($u_sensorid, $u_token, $u_interval, $u_unit, $u_resolution) {
$this->sensorid = $u_sensorid;
$this->token = $u_token;
if (empty($u_interval)) {
$this->interval = 'hour';
} else {
$this->interval = $u_interval;
}
if (empty($u_unit)) {
$this->unit = 'watt';
} else {
$this->unit = $u_unit;
}
$this->resolution = $u_resolution;
$this->getdata();
}
private function getdata() {
<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=array();
$header[]="Accept: application/json";
$header[]="X-Version: 1.0";
$header[]='X-Token: '.$this->token;
$request=$this->url.$this->sensorid.'?interval='.$this->interval.'&unit='.$this->unit.'&resolution='.$this->resolution;
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$request);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$this->data=curl_exec($ch);
curl_close($ch);
}
public function __toString() {
return $this->data;
}
}
$fluksodata = new Flukso($sensorid, $token, $interval, $unit, $resolution);
$fluksodata=json_decode($fluksodata);
$link = mysql_connect($db_host, $db_user, $db_pass)
or die ("Error ".date('Y-m-d H:i:s')."Failed to connect to MySQL.\n<b>A fatal MySQL error occured</b>.\nQuery: " . $query . "\nError: (" . mysql_errno($link) . ") " . mysql_error($link). " Affected rows is ".mysql_affected_rows($link));
mysql_select_db ($db_name)
or die ("Error ".date('Y-m-d H:i:s')."Failed to connect to the database ".$db_name.".\n<b>A fatal MySQL error occured</b>.\nQuery: " . $query . "\nError: (" . mysql_errno($link) . ") " . mysql_error($link). " Affected rows is ".mysql_affected_rows($link));
foreach($fluksodata as $value){
if ( "$value[1]" != "nan" )
{
$corrected_ts = $value[0] - 3660;
$formatdatetime = date('Y-m-d H:i:s', $corrected_ts);
$year = date('Y', $corrected_ts);
$corrected_value = $value[1];
$query = "insert ignore into $table (ts, value) values ('$formatdatetime', $corrected_value)";
$result = mysql_query ($query)
or die ("Error ".date('Y-m-d H:i:s')."Failed to insert ".$table." record.\nA fatal MySQL error occured.\nQuery: " . $query . "\nError: (" . mysql_errno($link) . ") " . mysql_error($link). " Affected rows is ".mysql_affected_rows($link));
}
}
mysql_close($link);
?>
At the moment the table in mysql looks like this
I changed the code to work and put the data correctly in mysql just missing the code to not copy values if they already existed in the base