How to write a REST API?

2019-01-03 20:11发布

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.

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.

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.

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.

10条回答
Luminary・发光体
2楼-- · 2019-01-03 20:37

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:

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

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.

How To Synchronize Core Data with a Web Service – Part 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:

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/

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

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-01-03 20:43

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

Hope this helps.

查看更多
女痞
4楼-- · 2019-01-03 20:50

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

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.

查看更多
兄弟一词,经得起流年.
5楼-- · 2019-01-03 20:51

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:

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:

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

查看更多
Fickle 薄情
6楼-- · 2019-01-03 20:53

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

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.

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

查看更多
欢心
7楼-- · 2019-01-03 20:57

I have programmed a REST API in ZEND Framework using 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.

EDIT: There seems to be no official documentation on Zend_Rest_Controller. This link describes 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.

查看更多
登录 后发表回答