Files
DomFramework/rest.php
2014-03-24 19:44:34 +00:00

37 lines
1.1 KiB
PHP

<?php
/** DomFramework
@package domframework
@author Dominique Fournier <dominique@fournier38.fr> */
require_once ("domframework/http.php");
/** Allow to manage the REST protocol by using the users output types */
class rest
{
/** Allowed types by default */
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
@param string $message Message to be displayed by the output type
@param integer $code HTTP code to use */
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);
}
}