50

I’m creating my first API to which if two values are passed, I should get the response in the JSON format. The number will be passed as parameters by POST. Either using cURL or whatever POST method is available.

Even though this is a very basic one, I would like to know the best practices and the API should be created by model–controller basis. Not just plain PHP.

I have Googled for many REST API tutorials. They were good and I have acquired some knowledge on it.

But I would like to get a sample model of the code so that I can refer to it and build my own, and that sample of course in the standard practice of making a real REST API.

If you ask me what I have tried, it would be really fun, as a beginner, all I could do is this:

$num1 = $_REQUEST['num1'];
$num2 = $_REQUEST['num2'];

$total = $num1 + $num2;
echo json_encode($total);

Of course this can never be called an API, but still. If I give a POST response to this, I want the response from the REST API as JSON. I should be able to test it by REST console as well so that I get a standard response.

Please do provide me with a very basic but yet a standard RESTful API.

Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100
Piya
  • 1,134
  • 4
  • 22
  • 42
  • Have a look at https://github.com/peej/tonic – Sergiu Paraschiv Feb 22 '14 at 19:34
  • @GajusKuizinas Entirely different from what I asked.Do read their questions before you comment – Piya Feb 22 '14 at 19:37
  • 3
    @PiyaSharma You have asked someone to make research and write the code for you. – Gajus Feb 22 '14 at 19:38
  • @GajusKuizinas I Dont think so :)..and sorry if you took it that way.I didnt meant it anyways – Piya Feb 22 '14 at 19:39
  • @PiyaSharma: What is this - **"`API should be created by model–controller`"**? Giving an architecture to your API resides in your own hand. – Veer Shrivastav Apr 16 '14 at 09:36
  • 1
    Just wanted to clarify that this is closer to an RPC service than a true REST service.. no? You just put in some data and get a response, without utilizing the verbs HTTP provides for instance. – Erik May 05 '14 at 18:44
  • 2
    The magic of PHP is the simple ways one can do simple things. If you want to needlessly overcomplicate your code, have a look at Java. –  May 23 '14 at 07:33
  • 1
    @Piya Try this https://github.com/mevdschee/php-crud-api Worked fine – Phillip Kigenyi Jan 02 '16 at 18:44
  • There is a lot of confusion around what REST really is. What you really want is probably not REST. See here https://medium.com/@trevorhreed/you-re-api-isn-t-restful-and-that-s-good-b2662079cf0e – Trevor Apr 02 '16 at 00:05
  • roytuts.com/rest-api-crud-example-in-php-mysql/ – user3470953 Jun 08 '17 at 13:02

2 Answers2

73

In your example, it’s fine as it is: it’s simple and works. The only things I’d suggest are:

  1. validating the data POSTed
  2. make sure your API is sending the Content-Type header to tell the client to expect a JSON response:

    header('Content-Type: application/json');
    echo json_encode($response);
    

Other than that, an API is something that takes an input and provides an output. It’s possible to “over-engineer” things, in that you make things more complicated that need be.

If you wanted to go down the route of controllers and models, then read up on the MVC pattern and work out how your domain objects fit into it. Looking at the above example, I can see maybe a MathController with an add() action/method.

There are a few starting point projects for RESTful APIs on GitHub that are worth a look.

Martin Bean
  • 38,379
  • 25
  • 128
  • 201
  • I recommend using the slim 3 framework for writing a REST API in php - worth a look at least :) Mostly because you can add your authentication and ACL as middleware layers (and content negotiation) which is just awesome. – John Hunt Jul 14 '16 at 09:28
  • @JohnHunt or even better: combine an automatic REST API with SlimPHP as described [here](https://tqdev.com/2019-automatic-api-slimphp-3). NB: I'm the author of that post. – mevdschee Apr 22 '19 at 17:36
17

Trying to write a REST API from scratch is not a simple task. There are many issues to factor and you will need to write a lot of code to process requests and data coming from the caller, authentication, retrieval of data and sending back responses.

Your best bet is to use a framework that already has this functionality ready and tested for you.

Some suggestions are:

Phalcon - REST API building - Easy to use all in one framework with huge performance

Apigility - A one size fits all API handling framework by Zend Technologies

Laravel API Building Tutorial

and many more. Simple searches on Bitbucket/Github will give you a lot of resources to start with.

Nikolaos Dimopoulos
  • 11,495
  • 6
  • 39
  • 67