Я сделал этот код с помощью:
https://github.com/influxdata/influxdb-php
https://www.highcharts.com/docs/working-with-data/live-data
<?php
include('/opt/lampp/htdocs/example/vendor/autoload.php');
$host = '127.0.0.1';
$port = 8086;
$dbname = 'aTimeSeries';
/*
$client = new \InfluxDB\Client($host, $port);
$database = $client->selectDB('aTimeSeries');
*/
//the exact same thing
$database = \InfluxDB\Client::fromDSN(sprintf('influxdb://user:pass@%s:%s/%s', $host, $port, $dbname));
//query of the last value
$result = $database->query('select * from valeurs group by * order by desc limit 1');
//recup le point
$points = $result->getPoints();
// Set the JSON header
header("Content-type: application/json");
// The x value is the current JavaScript time, which is the Unix time multiplied by 1000.
$x = time() * 1000;
// The y value is a random number
//$y = rand(0,100);
$y = $points;
// Create a PHP array and echo it as JSON
$ret = array($x, $y);
echo json_encode($ret);
?>
Выход:
[1523603506000,[{"time":"2018-04-13T07:11:45.208943754Z","value":48}]]
Но хотелось бы этого:
[1523603506000,48]
Или это:
[2018-04-13T07:11:45.208943754Z,48]
Если я попытаюсь вывести только $points, я получу только последнюю часть массива, но это тоже не то, что мне хотелось бы.
PS: если у вас есть лучшее решение для того же, может быть, с nodeJS, я обязательно послушаю
Да, это. И я бы хотел оставить только последнее значение 48.
Разве вы не можете просто получить доступ к value в JSON? Я не очень хорошо разбираюсь в php, но мне кажется, что должен быть способ вызвать значение по имени. (Возможно, $points.value? Что-то в этом роде?)
Это то, что я пытаюсь найти, но мои исследования пока не увенчались успехом.
Может, $test = json_decode($points)echo $test->value? Я предполагаю, что getPoints() возвращает JSON
Закройте @Codeer, это на самом деле echo $test[0]->value; (в JSON есть массив верхнего уровня)
Итак, решение - $y = json_decode($points); $ret = array($x, $y[0]->value);
Я пробовал, но: json_decode () ожидает, что параметр 1 будет строкой, заданным массивом ... $ points - это массив, но когда я попробовал echo implode (",", array_keys ($ points)); , Не возвращает ключи
Похоже, я неверно истолковал, где находится массив верхнего уровня. Попробуйте $y = json_decode($points[0]); $ret = array($x, $y->value);
Хорошо, просто найдите решение, вы все хорошие ребята, $ points = json_encode ($ points); $ points = json_decode ($ points); Странно, но работает.






Спасибо за вашу помощь, У меня была еще одна проблема, но я ее решил.
Но все еще есть 2 часа отставания, но теперь оно отображается прямо на высоких диаграммах.
<?php
include('/opt/lampp/htdocs/example/vendor/autoload.php');
$host = '127.0.0.1';
$port = 8086;
$dbname = 'aTimeSeries';
//directly get the database object
$database = \InfluxDB\Client::fromDSN(sprintf('influxdb://user:pass@%s:%s/%s', $host, $port, $dbname));
//query of the last value
$result = $database->query('select * from valeurs group by * order by desc limit 1');
//get the point
$points = $result->getPoints();
//make the array right
$points = json_encode($points);
$points = json_decode($points);
//take only the seconds of the time
$points[0]->time = substr($points[0]->time, 0,20);
// Set the JSON header
header("Content-type: text/json");
// The x value is the current JavaScript time, which is the Unix time multiplied by 1000 and take the time
$x = strtotime($points[0]->time) * 1000;
// The y value take the value which is a random value
$y = $points[0]->value;
// Create a PHP array and echo it as JSON
$ret = array($x, $y);
echo json_encode($ret);
?>
Что делает / возвращает ваш
getPoints()? Потому что я думаю, что возвращается[{"time":"2018-04-13T07:11:45.208943754Z","value":48}]]