3

I'm writing my first API, but I think I screwed it up from the design. It's a simple CRUD API which behave like this:

  • READ: /api/data gets redirected to api.php?data1=data. POST variables: user, HMAC, action (read).
  • CREATE: /api/newgets redirected to api.php. POST variables user, HMAC, action, data1, data2, data3.
  • DELETE: api/delete gets redirected to api.php. POST variables: user, HMAC, action, data1.
  • UPDATE: api/update gets redirected to api.php. POST variables: user, HMAC, action, data1, data2, data3.

Then in my api.php I have for if statements for performing each action based on $_POST['action']

However... is this an API? The READ behaves as expected I guess, but not sure if the other methods comply to the definition of an API.

closed as primarily opinion-based by Ixrec, user40980, Scant Roger Nov 30 '15 at 2:03

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    "API" does not have a precise definition, and I'm not aware of a vague definition of it that would exclude a typical REST setup like this. What definition are you thinking of? – Ixrec Nov 30 '15 at 0:02
  • If you send a GET request for read which includes POST parameters... yes, you're doing something wrong. POST requests should by convention only be used for create in a CRUD API. – Stephan Bijzitter Jan 24 '16 at 22:20
  • @StephanBijzitter actually you have no clue on this one. – user205863 Jan 25 '16 at 9:08
7

An API, probably yes. A REST API probably not.

If it works and another program can interact with it then it's an API (Application Programming Interface) IMHO.

However, your implementation can probably be improved.

It looks like you are going Restful. Typically HTTP methods (GET, POST, PUT, DELETE...) and used on a single URL that represents your entity.

Example:

GET /api/v1/widgets/1234

Would return widget with id=1234

POST /api/v1/gadgets

Creates a new widget. Data/payload would be sent in the body of the post as form data, json etc.

In your PHP code (or whatever language) use the HTTP method in the incoming request context to switch to a function to handle each method.

This is basically an MVC type pattern. There'll be plenty of articles on the Web that explain this in detail.

Generally these days there are frameworks that make life easier. Examples are ASP.NET Web API and Restify for NodeJS. Unfortunately, I don't know much about the PHP ecosystem. This article may help http://www.gajotres.net/best-available-php-restful-micro-frameworks/