State-API: Unterschied zwischen den Versionen

Aus Maschinendeck Wiki
Zur Navigation springen Zur Suche springen
>Problame
Created page with "{{Project |Why=Maschinenlesbarer Hackerspcae |What=software |Who=Problame |Status=running |Tags=php, quickhack }} Ein dreckiger Gammelfleischhack in PHP, weil schnell was her..."
 
>Ranlvor
Protokoll dokumentiert
Zeile 11: Zeile 11:


→ https://github.com/maschinendeck/state.maschinendeck.org
→ https://github.com/maschinendeck/state.maschinendeck.org
== Protokoll/Howto Raumstatus ==
* Zugangsdaten von [[User:Problame|Problame]] geben lassen
* Zum Raumstatus setzen: Mache einen HTTPS-Request gegen http://state.maschinendeck.org/update.php mit HTTP-Basic-Auth und POST-Parameter open=0 für zu oder open=1 für offen
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;
  }

Version vom 17. Oktober 2016, 17:29 Uhr

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