State-API

Aus Maschinendeck Wiki
Version vom 17. Oktober 2016, 17:29 Uhr von >Ranlvor (Protokoll dokumentiert)
Zur Navigation springen Zur Suche springen
State-API
Weshalb Maschinenlesbarer Hackerspcae
Was software
Wer Problame
Status läuft


Ein dreckiger Gammelfleischhack in PHP, weil schnell was her musste, um als Endpoint für die SpaceAPI sowie unsere Website herzuhalten.

Es geht darum, Metadaten über den Hackerspace maschinenlesbar bereitzustellen.

https://github.com/maschinendeck/state.maschinendeck.org

Protokoll/Howto Raumstatus

PHP Beispielcode:

  function setRaumstatus($newState) {
   global $APIUSER, $APIPASSWORD;
   $url = 'http://state.maschinendeck.org/update.php';
   $data = array("open" => $newState);
  
   // use key 'http' even if you send the request to https://...
   $options = array(
       'http' => array(
           'header'  => "Content-type: application/x-www-form-urlencoded\r\nAuthorization: Basic ".base64_encode($APIUSER.':'.$APIPASSWORD)."\r\n",
           'method'  => 'POST',
           'content' => http_build_query($data),
       ),
   );
   $context  = stream_context_create($options);
   $result = file_get_contents($url, false, $context);
  
   if($state = json_decode($result))
       if($state->status == 200)
           return true;
   
   return false;
  }