A
A
Alexander2012-09-13 18:24:25
PHP
Alexander, 2012-09-13 18:24:25

How to write an API?

Hello!

There was a task to write API for communication of application (ios) with a database. There is a lot of experience with php, but I didn’t have to write api. I would like to read about how to write such a layer.

In fact, nothing is complicated and the principle of operation seems to be clear. But I want to do everything correctly, so I ask the community to share links or personal experience or any other useful material on the topic.

In return, I promise to write an article on this topic :)

Thanks to all who responded!

Answer the question

In order to leave comments, you need to log in

6 answer(s)
E
Evgeny Glotov, 2012-09-19
@KIVagant

1. Rigid typing and control of input and output data. We got a lot of problems when the php server returned a string instead of an int, for example.
2. Provide a different format for the returned data - json, xml, etc. - not only ios can be on the clientside.
3. Provide for the interception of all internal errors and exceptions so that the client always receives a logical response instead of unexpected errors.
4. REST is trendy but completely optional. After several years of existence of some services, we are just now starting to introduce support for REST. This does not prevent us from developing popular mobile applications on all types of devices.
5. Study good examples of services and vice versa - terrible examples (Facebook). Never and never do like FB.
6. Provide debugging tools.
7. Consider versioning right away. Once a mobile application is released, it will no longer be possible to simply change the API.
8. You can immediately lay the caching and authorization tools at the API core level. For example, in one application, we passed an oauth token in headers. This made it possible to get rid of constant authorization checks inside API modules, giving it to parent classes and controlling access through configs.
Etc.

S
Sergey, 2012-09-13
Protko @Fesor

There are enough articles, of course you can write, but it’s unlikely that something new will come out.
It is better to discuss the structure of methods, what and how should return with the iOS developer, who will then implement this matter. If there is no such thing, break everything into atomic operations as much as possible, simplify the interaction, figure out for yourself what methods you might need (imagine that you are writing an API not for someone, but, for example, for a page that pulls everything through AJAX).
Regarding the implementation - banal serialization instead of rendering, there are no more differences. There are a bunch of out-of-the-box components that are well suited for serialization back and forth. Your own such component is also written quite quickly.
For authorization, an authorization token is usually used. Like, during authorization, the user is given a unique token (random string), which he then sends in the headers every time he accesses the API methods. This can be done as a separate layer.
I have not seen a single RESTfull API for serious applications, yes, yes, it is REST but not completely, so you should not bother here. It is enough just to process some basic headers and GET / POST requests. GET for selections - Tobish data in the database does not change when requested, except perhaps some counters, and POST to create records in the database (according to Feng Shui, the result of the function should return only HTTP headers, among which there is a GET request with the URI of the new object, but in practice nobody cares and returns the whole object or part of it).
Of course, you can use SOAP apishki, but from experience I will say that it is only suitable for developing very simple APIs, and there is little sense from it. If the client, of course, is an application written in C # .NET - then boldly SOAP and only SOAP, you will essentially have a minimum difference in implementation (in the form of time), and it will be much easier for the client developer. But on iOS with SOAP, everything is rather sad.

K
karazyabko, 2012-09-13
@karazyabko

First of all, read about the principles of REST (well, or Soap, depending on what you use). Well, you can take advantage of the experience of habratova:
One
Two

N
nill, 2012-09-13
@nill

There is a very good module for Node.js, from which you can peep interesting ideas for implementing a RESTful API - restify . I also advise you to see how the Amazon API is implemented . Keep in mind that these are implementations for enterprise use, and real life can be done with simpler solutions, for example, discarding unnecessary http headers.

P
Pavel Osipov, 2012-09-13
@Pavel_Osipov

As far as I know, the most popular paradigm for creating an API at the moment is REST
. I found the following articles on the experience of creating an API: 1 , 2 .
From experience I can say that the most important thing here is to plan everything correctly. Find a project as close as possible to your target that provides documentation on its API, and read it.

E
EugeneOZ, 2012-09-14
@EugeneOZ

Here is a VERY good article on how to write a REST API correctly:
blog.mugunthkumar.com/articles/restful-api-server-doing-it-the-right-way-part-1/
highly recommended.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question