Please disable Adblockers and enable JavaScript for domain CEWebS.cs.univie.ac.at! We have NO ADS, but they may interfere with some of our course material.

REST

rest_handle.php 
 
<?php
 function handleREST($server,$get) {
   $url = (array_key_exists('PATH_INFO',$server) ? $server['PATH_INFO'] : '/');
   $method = $server['REQUEST_METHOD'];
   parse_str(file_get_contents('php://input'),$contentargs);
   $arguments = array_merge($get,$contentargs);
   $accept = array_key_exists('HTTP_ACCEPT',$server) ? $server['HTTP_ACCEPT'] : '*/*';
   $ret = new StdClass;
   $ret->url = $url;
   $ret->method = $method;
   $ret->arguments = $arguments;
   $ret->accept = $accept;
   $ret->headers = getallheaders();
   return $ret;
 }
?>
 
server.php 
 
<?php
  include('rest_handle.php');
  $rest = handleREST($_SERVER,$_GET);
  print_r($rest);
    
  ### POST auf http://test.univie.ac.at/interop/studi_server_rest.php/bla/blo?a=17&b=35
  ### POST auf http://test.univie.ac.at/interop/studi_server_rest.php/bla/blo
  ###   for this POST when done the right way (like in an HTML Form):
  ###   Content-type: application/x-www-form-urlencoded
  ###   Body looks like:    
  # if ($rest->method == 'POST' && $rest->url = '/bla/blo/') {
  #   $rest->arguments['a'] => 17
  #   $rest->arguments['b'] => 35
  # }
  exit; # just that nothing that comes after is in the return result
?>
 
client.php for GET 
 
<?php
  header('content-type: text/plain');
  $result = file_get_contents('http://solo.wst.univie.ac.at/interop/studi_server_rest.php/bla/blo?a=b&b=c');
 
  print_r($result);
?>
 
client.php for POST, or PUT (just replace below) 
 
<?php
  $data = http_build_query(
    array(
      'var1' => 'some content',
      'var2' => 'doh',
    )
  );
  $opts = array('http' =>
    array(
      'method' => 'POST',
      'header' =>
        "Content-type: application/x-www-form-urlencoded\r\n" .
        "accept: application/json\r\n"
      ,
      'content' => $data
    )
  );
  header('content-type: text/plain');
  $context = stream_context_create($opts);
  $result = file_get_contents('http://solo.wst.univie.ac.at/interop/studi_server_rest.php/bla/blo',false,$context);
 
  print_r($result);
?>
 
client.php for DELETE 
 
<?php
  $opts = array('http' =>
    array(
      'method' => 'DELETE',
      'header' =>
        "Content-type: application/x-www-form-urlencoded\r\n" .
        "accept: application/json\r\n"
    )
  );
  header('content-type: text/plain');
  $context = stream_context_create($opts);
  $result = file_get_contents('http://solo.wst.univie.ac.at/interop/studi_server_rest.php/bla/blo',false,$context);
 
  print_r($result);
?>
 
How to use REST parameters: 
 
http://test/a/b/
 
<a>
 <b/>
 <c/>
 <d/>
</a>
 
http://test/a/b/?some=1 (parameters are a filtering criteria for the full result)
 
<a>
 <b/>
</a>
Letzte Änderung: 07.05.2020, 12:22 | 235 Worte