I'd like to automatically generate a web API from a database. I can easily query the database with SQL, but obviously this won't do for a public API. I can also write the server-side code manually, but this will take a very long time, since the database is fairly large. Also, writing it manually would be a very mechanical and repetitive task, so I don't see why a program can't do it for me.
For example, let's say I have a table in my database called "Announcements". I'm looking for a way to (given that database table), automatically generate the server-side code that will allow someone to create new announcements, requiring the correct fields. Done manually, this could look like a PHP file called "create_announcement.php", which requires all the fields that exist in the database, like the announcement title, body, and author. Of course, this could also be done in a cleaner way by following some design model (like REST), but this is meant as an example.
Other things I'm considering are:
So what kind of tools should I look into? What is the general approach people take when confronted with this problem?
The comments above have listed out some great existing software, you've really got to figure out whether or not you want to reinvent this concept.
If you write this yourself you can still save a lot of time vs writing each page & action individually, but the validation you're talking about is probably going to eat up all of your time if you don't already have that functionality built into these models, if they exist.
Something simple could take in a table name, action name, id number from the URL and spit out basic stuff.
<?
$allowed_tables = array('posts','comments','people');
$table = $_GET['table'];
$page = (int) $_GET['page'];
$per_page = 100;
$offset = ($page - 1) * $per_page;
if (!$table || !in_array($table, $allowed_tables)) {
die('Invalid table.');
}
$rs = mysql_query("SELECT * FROM `$table` ORDER BY `id` DESC LIMIT $per_page, $offset");
$data = array();
if ($rs && mysql_num_rows($rs)) {
while ($row = mysql_fetch_array($rs,MYSQL_ASSOC)) {
$data[] = $row;
}
}
print json_decode($data);
?>
You could definitely make some serious progress in a couple of hours, adding in fields to specifically show or hide, you could even deal with the validation on a simple basis (required fields would be simple) for now just to get it rolling.