State-API

From Maschinendeck Wiki
Jump to: navigation, search
State-API
Why Maschinenlesbarer Hackerspcae
What software
Who Problame
Status running
Tags php, quickhack

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

curl Beispielcode

   curl -v -H "Content-type: application/x-www-form-urlencoded" -H "Authorization: Basic base64_encode($APIUSER.':'.$APIPASSWORD)" -d 'open=0' -X POST http://state.maschinendeck.org/update.php

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;
  }