php 如何编写 REST API?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4973156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:24:02  来源:igfitidea点击:

How to write a REST API?

phpiphonemysqlrest

提问by Erik B

I'm writing an iPhone app as a hobby project and it will need a web service to provide it with data. It's not very different from what I do at work, but at work I only write views and controllers. Someone else is responsible for writing the model and usually the clients provide the web service.

我正在编写一个 iPhone 应用程序作为一个爱好项目,它需要一个网络服务来为其提供数据。这和我在工作中所做的并没有太大区别,但是在工作中我只编写视图和控制器。其他人负责编写模型,通常由客户端提供 Web 服务。

I have done some web programming before, back when everyone were using MySQL and PHP, so my skills are a bit outdated, but I'm confident that I would be able to pull it of using the techniques I already know. However, I don't want to waste my time using obsolete tools. I've figured out that the state of the art would be to write a REST API. I was thinking that there should be some pretty good frameworks out there that pretty much just gives you a REST API with CRUD functionality as soon as you've defined a model.

我以前做过一些 web 编程,那时候每个人都在使用 MySQL 和 PHP,所以我的技能有点过时,但我有信心我能够使用我已经知道的技术来拉它。但是,我不想浪费时间使用过时的工具。我发现最先进的方法是编写 REST API。我在想应该有一些非常好的框架,只要你定义了一个模型,它们就会为你提供一个具有 CRUD 功能的 REST API。

I guess my question is: What would be the fastest way to get a REST API up and running? I really just want to focus on writing the iPhone app and not spend too much time on this API. It would be great if I could get web administration and revision history too. I should also add that the API isn't supposed to be public, so support for authentication would be great as well.

我想我的问题是:启动和运行 REST API 的最快方法是什么?我真的只想专注于编写 iPhone 应用程序,而不是在这个 API 上花太多时间。如果我也能获得网络管理和修订历史,那就太好了。我还应该补充一点,API 不应该是公开的,因此对身份验证的支持也会很好。

Just to be clear. I wouldn't mind a PHP framework. In fact it could possibly be better since I know that my current hosting supports it.

只是要清楚。我不介意 PHP 框架。事实上,它可能会更好,因为我知道我当前的托管支持它。

采纳答案by Erik B

Just to let you know:

只是让你知道:

I ended up using Ruby on Rails.

我最终使用了 Ruby on Rails。

EDIT:Since this answer has been downvoted for not providing the reason behind choosing Ruby on Rails and also no instructions on how to write a REST API with it, I thought I would give you my motivation and some simple instructions.

编辑:由于这个答案因为没有提供选择 Ruby on Rails 背后的原因而被否决,也没有关于如何用它编写 REST API 的说明,我想我会给你我的动机和一些简单的说明。

I started reading a book about Ruby on Rails and realized that all I needed to do was to use scaffolding and I got a JSON REST API for free.

我开始阅读一本关于 Ruby on Rails 的书,并意识到我需要做的就是使用脚手架,而且我免费获得了一个 JSON REST API。

Here's a good guide to get you started: http://guides.rubyonrails.org/getting_started.html

这是一个很好的入门指南:http: //guides.rubyonrails.org/getting_started.html

When you have your Ruby on Rails environment up and running, creating your REST API isn't harder than running:

当您的 Ruby on Rails 环境启动并运行时,创建 REST API 并不比运行更难:

$ rails generate scaffold Post name:string title:string content:text

(Example from the above link.) I also found that Rails is very easy and free to deploy to heroku, which meant that I didn't have to pay for hosting for my very basic, low traffic, REST API. There are many other reasons why I am very happy to work with Ruby on Rails, but that's beyond the context of this question.

(来自上述链接的示例。)我还发现 Rails 非常容易并且可以免费部署到heroku,这意味着我不必为我的非常基本的、低流量的 REST API 支付托管费用。我很高兴使用 Ruby on Rails 的原因还有很多,但这超出了本问题的范围。

回答by nickfox

EDIT:

编辑:

The links below which apparently were good for 3 years are no longer working so I went and found a couple of new tutorials that I think are going to stick around for a while. These are on the Ray Wenderlich site, a very well respected ios dev tutorial site. The first article actually references the broken links below but it is complete within itself:

下面显然可以使用 3 年的链接不再有效,所以我去找了一些我认为会保留一段时间的新教程。这些都在 Ray Wenderlich 站点上,这是一个非常受尊敬的 ios 开发教程站点。第一篇文章实际上引用了下面的断开链接,但它本身是完整的:

How To Write A Simple PHP/MySQL Web Service for an iOS App

如何为 iOS 应用程序编写简单的 PHP/MySQL Web 服务

and the second one has a little twist to it. It used parse.com on the backend and AFNetworking. Both of which are quite excellent.

第二个有一点点扭曲。它在后端使用 parse.com 和 AFNetworking。两者都非常出色。

How To Synchronize Core Data with a Web Service – Part 1

如何将核心数据与 Web 服务同步 - 第 1 部分



I have fixed the broken links below by finding the articles in the way back machine. People seem to like the links so I will keep them. The links above should provide more food for thought.

我通过在返回机器的路上找到文章修复了下面的断开链接。人们似乎喜欢这些链接,所以我会保留它们。上面的链接应该提供更多的思考。



I am doing exactly the same thing with my iphone app. I found this article on building a RESTful API in PHP:

我正在用我的 iphone 应用程序做同样的事情。我发现这篇关于在 PHP 中构建 RESTful API 的文章:

https://web.archive.org/web/20130910164802/http://www.gen-x-design.com/archives/create-a-rest-api-with-php/

https://web.archive.org/web/20130910164802/http://www.gen-x-design.com/archives/create-a-rest-api-with-php/

and there is also a followup article here:

这里还有一篇后续文章:

https://web.archive.org/web/20130323001500/http://www.gen-x-design.com/archives/making-restful-requests-in-php/

https://web.archive.org/web/20130323001500/http://www.gen-x-design.com/archives/making-restful-requests-in-php/

with a link to source code at the bottom of the article.

文章底部有源代码链接。

回答by Philipp Flenker

I have programmed a REST API in ZEND Frameworkusing the Zend_Rest_Controller, on the iPhone I used ASIHTTPRequest. My experience with both where good. At the beginning I had some trouble setting up ZEND and connecting it to mySQL, but once I figured out how to do it I was able to write the API very quickly. I can share more information with you if you have any further questions.

我使用 Zend_Rest_Controller在ZEND 框架中编写了一个 REST API ,在我使用的 iPhone 上使用ASIHTTPRequest。我在这两个方面的经验都很好。一开始我在设置 ZEND 并将其连接到 mySQL 时遇到了一些麻烦,但是一旦我弄清楚了如何去做,我就能够非常快速地编写 API。如果您有任何其他问题,我可以与您分享更多信息。

EDIT: There seems to be no official documentation on Zend_Rest_Controller. This linkdescribes how to use it to create your API. You simply have to disable rendering in the init()of your subclass and implement the methods for each REST call.

编辑:似乎没有关于Zend_Rest_Controller. 此链接描述了如何使用它来创建您的 API。您只需在init()您的子类中禁用渲染并为每个 REST 调用实现方法。

回答by jim_kastrin

I followed a quite simple tutorial for creating RESTful APIs with PHP:

我遵循了一个使用 PHP 创建 RESTful API 的非常简单的教程:

Corey Maynard - Creating a RESTful API with PHP

Corey Maynard - 使用 PHP 创建 RESTful API

The main concept includes:

主要概念包括:

  • one abstract class that handles the parsing of the URI and returning the response, and
  • one concrete class that consists of just the endpoints for the API.
  • 一个处理URI解析并返回响应的抽象类,以及
  • 一个具体的类,只包含 API 的端点。

回答by igorgue

What about Python?

Python呢?

I'd use Python, Django and Piston.

我会使用 Python、Django 和 Piston。

  1. I'd generate Django models from your existent DB using inspectdb.
  2. Add the Django adminto your models.
  3. Add Django Pistonto your app.
  4. Profit.
  1. 我会使用inspectdb从您现有的数据库生成 Django 模型。
  2. Django 管理员添加到您的模型中。
  3. Django Piston添加到您的应用程序中。
  4. 利润。

With no experience with Python or Django probably it'll take you a day to develop this solution and all code is unit tested and proved to work.

如果没有 Python 或 Django 经验,您可能需要一天时间来开发此解决方案,并且所有代码都经过单元测试并证明有效

回答by adamkrell

If you want to use PHP I recommend using the CodeIgniter framework with Phil Sturgeon's REST server:

如果您想使用 PHP,我建议您使用带有 Phil Sturgeon 的 REST 服务器的 CodeIgniter 框架:

http://net.tutsplus.com/tutorials/php/working-with-restful-services-in-codeigniter-2

http://net.tutsplus.com/tutorials/php/working-with-restful-services-in-codeigniter-2

https://github.com/philsturgeon/codeigniter-restserver

https://github.com/philsturgeon/codeigniter-restserver

回答by shauvik

Checkout the following PHP class that follows MVC. http://www.phpclasses.org/package/5080-PHP-Implement-REST-Web-services-servers.html

查看以下遵循 MVC 的 PHP 类。 http://www.phpclasses.org/package/5080-PHP-Implement-REST-Web-services-servers.html

Hope this helps.

希望这可以帮助。

回答by Bogatyr

If you already know PHP, there's nothing wrong with a PHP/MySQL backend. You can send all responses in iPhone-compatible plist xml format, and instantly turn the response into a NSDictionary/NSArray/NSNumber data structure with this short snippet of code:

如果您已经了解 PHP,那么 PHP/MySQL 后端没有任何问题。您可以以 iPhone 兼容的 plist xml 格式发送所有响应,并使用以下简短代码段立即将响应转换为 NSDictionary/NSArray/NSNumber 数据结构:

NSString *response = [request responseString];
NSData* plistData = [response dataUsingEncoding:NSUTF8StringEncoding];
NSPropertyListFormat format;
NSString *errorStr;
NSDictionary* plist = [NSPropertyListSerialization propertyListFromData:plistData 
                                                       mutabilityOption:NSPropertyListImmutable 
                                                                 format:&format 
                                                       errorDescription:&errorStr];

I also use the ASIHTTP package for forming URLs, sending asynchronous requets, and receiving the responses, I highly recommend it:

我还使用 ASIHTTP 包来形成 URL、发送异步请求和接收响应,我强烈推荐它:

http://allseeing-i.com/ASIHTTPRequest/

http://allseeing-i.com/ASIHTTPRequest/

回答by Moshe

You should use whatever languages you are comfortable with for the web service. Any language that can formulate REST responses to requests is fine.

对于 Web 服务,您应该使用您熟悉的任何语言。任何可以为请求制定 REST 响应的语言都可以。

That said, if you want to get something running quickly, I suggest using Python on Google App Engine. It's free and you can use Java instead of Python if you so desire. App Engine supports authentication using OpenID and/or Google Accounts (not sure if they're mutually exclusive) so that should make things easier to code.

也就是说,如果您想让某些东西快速运行,我建议在 Google App Engine 上使用 Python。它是免费的,如果您愿意,可以使用 Java 代替 Python。App Engine 支持使用 OpenID 和/或 Google 帐户(不确定它们是否互斥)的身份验证,因此应该更容易编码。

As far as making the requests on the iOS device, I suggest using ASIHTTPRequest.

至于在 iOS 设备上发出请求,我建议使用ASIHTTPRequest

回答by Mark Sawers

Another option is restSQL, an ultra-lightweight persistence framework. See http://restsql.org. It supports MySQL and PostgreSQL and runs in a standard Java EE container, e.g. Apache Tomcat.

另一种选择是restSQL,一个超轻量级的持久化框架。请参阅http://restsql.org。它支持 MySQL 和 PostgreSQL,并在标准的 Java EE 容器中运行,例如 Apache Tomcat。

restSQL is a very unconventional data access layer. restSQL is not an object-oriented view of the database. It presents flat or hierarchical "views" of relational database tables. These views are query-able and updatable through a simple REST-based HTTP or Java API. The HTTP interface is based on REST principles, which use HTTP's built-in features, rather than abstracting away from them.

restSQL 是一个非常规的数据访问层。restSQL 不是数据库的面向对象视图。它呈现关系数据库表的平面或分层“视图”。这些视图可通过简单的基于 REST 的 HTTP 或 Java API 进行查询和更新。HTTP 接口基于 REST 原则,它使用 HTTP 的内置功能,而不是从它们中抽象出来。

You want a 'REST API with CRUD functionality' and that's exactly restSQL's sweet spot. You could do this with no code. Simply define your SQL Resources via XML files and start doing HTTP calls against them with full CRUD capability.

您想要一个“具有 CRUD 功能的 REST API”,而这正是 restSQL 的最佳选择。您无需代码即可完成此操作。只需通过 XML 文件定义您的 SQL 资源,并使用完整的 CRUD 功能开始对它们进行 HTTP 调用。