A Very Simple API 

development-api

There comes a time in the life of a developer where he/she is forced to deal with an API, be a desktop or a web developer, in some time it will be necessary.

hot-head

Also, maybe, the developer will need to create it’s own API and then is where the things get a little bit difficult. There is a lot of concerns, discussions and suggestions around API standards but none of them are still the definitive right way to build an API, so as always each developer builds the code as it wants.

For me, everytime that I need to deal with a webservice, I study a lot, check the new standards look at the client needs and make the best effort to make the most light and precise services that the client wants. But thats the hard way, the easy way should be, to unpack a generic API code adjust some settings and start to delivery services… Should be like this.

crazyHarry

There are some nice tutorials and tools on internet about building an API, like with laravel and with apigility, but almost all have some framework dependencies and then I though in something more simple, just with the basic stuff, why not?

 

Considering all that, and on all the effort spend on every new webservice, I decided to make a very simple API pack with the basics to make a webservice to work, so here is what I tried to covered:

  • Auto detect XML-RPC or REST request;
  • Standard formated XML or JSON automatically (depending of the request);
  • Automatic deal with errors and/or warnings (saving in a log);
  • Resources to return standard field answers on requests;
  • Code injection protection;
  • Method request control and run according to the operation method request (POST, GET, PUT, DELETE, OPTIONS, PATCH);
  • Versioning control;
  • URL friendly following at least level 2 maturity level;
  • Start point to build my own custom webservice methods;

It’s not easy to cover all the resources of an API and still keep the code simple and dependencies free but I tried. In the end the result was very good but still have some work to do.

The code is avalibale on my github account and it’s free to use and to contribute. Note that this is not the “state of art” and any kind of contribution will be welcome.

The very simple API pack worked just fine and  can be used on every new project with just a few lines of set up on an specific settings file. Also, the use is very simple:

  1. Extend the main class AbstractService:
    class MyServiceName extends \classes\AbstractService
  2. Initialize the service with the needed settings:

    public function execute()
    {
    $this->initialize(['allowedMethods'=>[AMT_GET, AMT_POST, AMT_OPTIONS],
    'answerType'=>AST_AUTO]);
    }
  3. Add the code on the respective method and call the answer method:

    public function onPost()
    {
    //... your code here
    $this->answer([
    'message' => 'Here are the fields that the API received',
    'fields' => $this->getFields()
    ]);
    }
  4. Call the API from the browser:
    http://your-server-path/v1/MyServiceName

… and voilà!

vsapi-sample-answer
Sample JSON answer using AJAX on vsapi

Also, an error message:

vsapi-sample-error-answer
JSON error answer using AJAX on vsapi

And, a XML answer:

vsapi-xml-answer
XML answer using PHP on vsapi