Skip to content

Instantly share code, notes, and snippets.

@mattsches
Created April 22, 2017 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattsches/4b84e0f1adc5f383a441800e8e163d17 to your computer and use it in GitHub Desktop.
Save mattsches/4b84e0f1adc5f383a441800e8e163d17 to your computer and use it in GitHub Desktop.
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