对动态网站使用RESTful API [英] Using a RESTful API for Dynamic Websites

查看:157
本文介绍了对动态网站使用RESTful API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP和MySQL

在构建动态网站(可以是个人网站,Web应用程序等)时,是否适合使用API​​请求调用而不是MySQL查询?

In building dynamic websites (can be personal, web app, etc..), is it suitable to use API request calls, instead of MySQL queries?

函数getArticles()中的示例,是否应使用本机MySQL查询,如:

Example in my function getArticles(), should I use a native MySQL query like:

$sql = 'SELECT id, title, content
        FROM r_articles'
$result = mysql_query($sql) or die ('.. bla bla');

或者,只需执行GET请求即可:http://mysite.com/api/articles.然后(假定响应为JSON)格式化响应.

Or, just execute a GET request to: http://mysite.com/api/articles. Then (supposed to be the response is in JSON) format the response.

注意:当然,首先要创建API.

NOTE: The API of course is to be created first.

推荐答案

这完全取决于您网站的类型. 如果您仅通过Web界面构建一个小型项目,请保留mysql调用.

That depends fully on the type of your site. If you're building a small project with just the webinterface, stay with your mysql calls.

如果您要建立的网站规模会不断扩大,并提供多种服务,例如一个android/iPhone应用程序已经被计划了,构建api并让两个应用程序都使用它. API非常适合于可伸缩性并为许多客户提供服务. 但是,对于网络表示,例如为您的本地牙医提供简单的管理费用.

If you're doing a site which shall grow big and serve more than one service, e.g. an android/iPhone application is already planned out, build the api and let both applications use it. APIs are great for scalability and serving to a lot of customers. However, for a web representation e.g. for your locals dentist its simply overhead.

这篇关于对动态网站使用RESTful API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆