简体   繁体   English

如何从数据库“生成”Web API?

[英]How do I “generate” a web API from a database?

I'd like to automatically generate a web API from a database. 我想从数据库自动生成Web API。 I can easily query the database with SQL, but obviously this won't do for a public API. 我可以使用SQL轻松查询数据库,但显然这不适用于公共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". 例如,假设我的数据库中有一个名为“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. 手动完成,这可能看起来像一个名为“create_announcement.php”的PHP文件,它需要数据库中存在的所有字段,如公告标题,正文和作者。 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. 当然,这也可以通过遵循一些设计模型(如REST)以更清洁的方式完成,但这只是一个例子。

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. 如果我可以生成一个API来通过HTTP访问数据库(正如我上面所描述的那样),API将不会意识到某些特定性。 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? 人们在面对这个问题时采取的一般方法是什么?

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. 简单的东西可以从URL中获取表名,操作名,ID号并吐出基本内容。

<?
$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. 你肯定可以在几个小时内取得一些重大进展,在字段中添加专门的显示或隐藏,你甚至可以在简单的基础上处理验证(必填字段很简单),现在只是为了让它滚动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从MySQL数据库生成站点地图 - How do I generate site map from MySQL database 如何通过mongolab rest api创建Web服务,以便在php中使用curl进行基本数据库操作? - how do I create web service through mongolab rest api for basic database operation using curl in php? 我如何生成将根据 ID 从数据库显示这些产品的链接到 php 中的另一个页面(产品页面) - How do i generate links that will display these products depending on the ID from the database to another page (product page) in php 如何自动生成基于 Yii 的 Web 应用程序的文档 - How do I auto generate documentation of Yii based web application 如何显示MySQL数据库中每个网页最多显示50个URL的列表? - how do I display list of URLS say maximum 50 per web page from MySQL database? 如何从Web服务器到新安装的WAMP获取有效的MySQL数据库 - How do I get a working MySQL database from my web server to my newly installed WAMP 如何将iPhone应用程序中的SQLite数据库中的数据发送到Web服务? - How do I send data from SQLite database in iPhone app to Web Service? 如何从数据库生成Excel报告? - How can I generate a excel report from a database? 如何从数据库中的表生成树结构? - How can I generate a tree structure from a table in a database? 如何在网页的一部分中创建可搜索的数据库? - How do i create a searchable database within a section of a web page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM