1

I want to make an API in php. I searched a lot of tutorials. Every tutorial uses json_encode() to return a json format of an array.

This array is made from the data we get from database.

I also am familiar a restful api must follow all the constraints defined in the REST design to be called a restful.

I want to know.

We are using json_encode to send data in json form, and using HTTP. It is an restful api?

We also use all the time HTTP in PHP. So everything is already restful?

What separates an restful api from an normal API?

What things are necessary for an API in PHP to be called restful?

I know it's silly, but I am so confused. I already went through many tutorials.

Regolith
  • 2,676
  • 9
  • 30
  • 42
deepdark
  • 73
  • 7
  • 1
    Is it restful if you use JSON and / or HTTP alone. No. Unsure what a "normal API" would be. The difference is how you design your API. Perhaps look through https://stackoverflow.com/questions/671118/what-exactly-is-restful-programming – Jonnix Jul 23 '19 at 09:50
  • @deepdark You have to study what exactly a RESTful API is first. Maybe start googling? – Raptor Jul 25 '19 at 06:55

2 Answers2

0

REST runs on top of HTTP so you're most of the way there. What a REST API returns has nothing to do with REST and is more to do with the application's domain.

You're using json_encode to return JSON, which is fine and is perfectly acceptable as a REST return type. Another REST API might return a compressed CSV if bandwidth is tight or the device calling the API is resource constrained. What it returns is of less importance than how it's called.

REST refers to representation. So the client wants a representation of a resource at the server. e.g. this:

/api/user/{name}

can return the representation of a user in JSON format using json_encode. e.g.

/api/user/smith

returns:

{
  "nane": "Mr. Smith",
  "email: "smith@smith.com"
}

if you use the REST verbs to work with that state then you're using REST.

The other aspect of REST that causes lots of debate is what the URLs look like. The URLs should ideally identify a resource and the verbs say how that resource should be interacted with, e.g. GET a user, POST a new user, PUT updates to the existing user, DELETE an existing user.

The operations performed on the representation of the resource are specified by the HTTP methods.

To answer your question about "normal" APIs, there aren't any. There is no API standard, however REST provides those two markers, resources as URLs and operations on those resources as HTTP verbs. Other types of API are xml-rpc where some people use that type of URL, e.g. POSTing data to:

/api/user/new

the above isn't a RESTful URL as it doesn't identify a resource. It identifes an operation (new user).

SOAP is another type of API that is far more complex than REST but it was designed to do things REST doesn't need to do, such as routing and service discovery.

So in summary, if you expose your resources as URLs and use HTTP verbs to interact with them, you're using REST. Looks like you're good to go.

codebrane
  • 3,374
  • 1
  • 14
  • 20
  • 1
    "There is no API standard" is not quite correct. There are some standards for building a REST API. You might consider HATEOAS not a standard but a design pattern but [JSON:API specification](https://jsonapi.org/) is definitely one. There are also some published [API Design Guidelines](http://apistylebook.com/design/guidelines/) used by enterprise companies to build there APIs. – jelhan Jul 23 '19 at 14:24
  • yes thanks for the clarification. There are best practice patterns but I wouldn't go as far as calling them 'standards'. Not yet anyway. JSON:API touches on the returned data from an API and is a good starting point unless the API returns XML but can still inform the returned data. It doesn't cover REST endpoint design which is closer to the question "is it restful?" and I suppose that is more to do with the domain/client/ecosystem. Thanks for the pointer to the design guidelines. I think's important to understand what REST is at a very simple level of endpoint design to get a feel for domains – codebrane Jul 23 '19 at 14:30
  • JSON:API specification comes with some [recommendations for URL design](https://jsonapi.org/recommendations/#urls) and enforces some REST rules on endpoint design (e.g. HTTP verbs and status codes). It's not only about representation a resource or a collection of resources in JSON. It's "a specification for how a client should request that resources be fetched or modified, and how a server should respond to those requests." – jelhan Jul 23 '19 at 14:34
0

The main difference between REST and a HTTP API with JSON response is that REST uses $_SERVER['REQUEST_METHOD'] values (POST/GET/PUT/DELETE) to differentiate between the create/read/update/delete actions. For an example, check out this small (66 line) program I've written to demonstrate the concept.

mevdschee
  • 1,331
  • 14
  • 15