Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a REST API?

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.

like image 982
Erik B Avatar asked Feb 11 '11 19:02

Erik B


People also ask

What is RESTful API example?

For example, a REST API would use a GET request to retrieve a record, a POST request to create one, a PUT request to update a record, and a DELETE request to delete one. All HTTP methods can be used in API calls. A well-designed REST API is similar to a website running in a web browser with built-in HTTP functionality.

Can I make my own REST API?

Creating your own RESTful API can be a great way to build a business around data you've collected or a service you've created, or it can just be a fun personal project that allows you to learn a new skill. Here's a list of 20 tutorials on how to design your own REST API!

How do I create a first REST API?

First, create a folder where you want to store your files and folders. Let's name test-api, you can name it whatever you want. Now go to that directory and run npm init -y in the command line. It will initialize an empty directory to use for node.

What is REST API written in?

REST technology is generally preferred over other similar technologies. This tends to be the case because REST uses less bandwidth, making it more suitable for efficient internet usage. RESTful APIs can also be built with programming languages such as JavaScript or Python.


2 Answers

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.

like image 164
nickfox Avatar answered Oct 02 '22 05:10

nickfox


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.

like image 45
Philipp Avatar answered Oct 02 '22 05:10

Philipp