Add the rest library : read the wanted output format, and display it if it is available

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@1229 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2014-03-21 15:45:20 +00:00
parent 7524ca29cc
commit 28a5d1b4ce

28
rest.php Normal file
View File

@@ -0,0 +1,28 @@
<?php
require_once ("domframework/http.php");
class rest
{
public $allowedtypes = array ("json", "xml", "txt", "csv");
/** Display the message (which can be a string, an array, an integer...)
into the type asked by the client if it is allowed.
By default, the JSON type is used */
function display ($message, $code=200)
{
$http = new http;
$text = $http->codetext ($code);
header ($_SERVER["SERVER_PROTOCOL"]." $code $text");
$type = reset ($this->allowedtypes);
if (isset ($_SERVER["HTTP_ACCEPT"]))
{
$type = $http->bestChoice ($_SERVER["HTTP_ACCEPT"], $this->allowedtypes,
$type);
}
require_once ("domframework/output$type.php");
$constr = "output$type";
$method = "out";
$obj = new $constr ();
$obj->$method ($message);
}
}