Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Einfache Abfrage des API von luftdaten.info basierend auf http://fuerles.de/index.php/it-welt/10-iot/59-feinstaub-sensor-api-lesen
<?php
/**
* Class LuftdatenApi
*/
class LuftdatenApi
{
const BASE_URL = 'http://api.luftdaten.info/v1/sensor/';
/**
* @param int $sensorId
* @return string
*/
public static function pickValues($sensorId)
{
$jsonResponse = file_get_contents(self::BASE_URL . $sensorId);
$response = json_decode($jsonResponse);
$result = array_pop($response);
/** @var array $sensorDataValues */
$sensorDataValues = $result->sensordatavalues;
$valueString = $sensorDataValues[0]->value_type . ': ' . $sensorDataValues[0]->value;
if (array_key_exists(1, $sensorDataValues)) {
$valueString .= ' ' . $sensorDataValues[1]->value_type . ': ' . $sensorDataValues[1]->value;
}
return $valueString;
}
}
$tweet = LuftdatenApi::pickValues(1778) . ' ' . LuftdatenApi::pickValues(1777);
$tweet = str_replace(['temperature', 'humidity', 'P1', 'P2'], ['Temp', 'Hum', 'P10', 'P2.5'], $tweet);
echo $tweet;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment