Created
April 22, 2017 14:12
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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