Search code examples
phpweb-servicesserver-side

How do I "generate" a web API from a database?


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:

  • Some form of authentication. For example, if the user would like to delete an announcement, he/she should be required to "log in" to do so. I'm not sure how this works here.
  • Some way to add logic. If I can generate an API to access the database through HTTP (as I tried to describe above), the API will not be aware of certain specificities. Let's say the announcement resource should include a date that must be after today. I need a way to express that in the code.
  • It would be ideal if the solution could follow some design model to make it easier to access from different clients.

So what kind of tools should I look into? What is the general approach people take when confronted with this problem?


Solution

  • 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.