Simple Rest
<?php
header('Cache-Control: no-cache, must-revalidate');
header('Expires: '.gmdate('D, d M Y H:i:s', time()).' GMT');
header('Content-type: application/json; charset=utf-8');
try {
$link = mysqli_connect(
'localhost',
'',
'',
''
);
if (mysqli_connect_errno($link)){
die('Could not connect: ' . mysqli_connect_error());
}
$method = $_SERVER['REQUEST_METHOD'];
switch ($method) {
case 'POST':
break;
case 'GET':
$restPath = explode("/",$_SERVER['REQUEST_URI']);
array_shift($restPath);
break;
default:
echo "Rest error";
break;
}
//Model
$model = ucfirst($restPath[0]);
require_once("./models/".$model.".php");
//Create object
$object = new $model($link);
$method = $restPath[1];
$methodParams = array_slice($restPath, 2);
$result = call_user_func_array(array($object, $restPath[1]), $methodParams);
echo json_encode($result);
} catch(Exception $e) {
echo "Exception: ", $e;
}