Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you use REST in PHP? If so how? [closed]

I am developing my own PHP Library and I would like to call RESTful web-services from my API. Can this be done in PHP and if so what are the basics in doing so?

like image 702
Zeck Avatar asked Oct 27 '09 03:10

Zeck


People also ask

Does PHP use REST?

Edit: Replaced "Php is rest by default" with "Since REST is the application of the same methods of the HTTP protocol to the design of client-server architectures and PHP is already so good to handle HTTP protocol requests such as GET and POST. PHP is specially suited to make developing REST services easy."

Is REST only for web?

No, its more like a design pattern applicable to client and server software. The clients and servers could use any protocol as long some form of hypertext messages are exchanged - no web or Internet required.

Is REST a protocol or not?

REST is a set of architectural constraints, not a protocol or a standard. API developers can implement REST in a variety of ways.


1 Answers

Since REST is the application of the same methods of the HTTP protocol to the design of client-server architectures and PHP is already so good to handle HTTP protocol requests such as GET and POST. PHP is specially suited to make developing REST services easy.

Remember REST is the application of the same http patterns that already exists.

So if you currently have an application that does something like:

  1. HTML Form
  2. PHP Process
  3. HTML Output in a table

So to make it REST you would need to:

  1. Accept parameters from the web. This is easy since you will receive the parameters either as get or post... so it is basically the same.

  2. PHP process

  3. Output in either JSON or XML. And that is it!

    Is pretty easy.

Now the difficult part is to make your API (the functions and URLs) that you will generate to be programmer friendly.

In that case I suggest you look at the flickr API as an example is very developer friendly easy to guess and has good documentation.

For more info on APIs look at this presentation: How to Design a Good API & Why it Matters (Joshua Bloch)

Finally a RESTful API should implement also the PUT and DELETE methods of the http protocol when it makes sense

For example if you had a delete action in your api, said service should receive the delete method from the http protocol. Instead of the more common thing of sending an action parameter as part of a post request.

Edit: Replaced "Php is rest by default" with "Since REST is the application of the same methods of the HTTP protocol to the design of client-server architectures and PHP is already so good to handle HTTP protocol requests such as GET and POST. PHP is specially suited to make developing REST services easy."

And also added the final note that you should implement the appropiate PUT or DELETE methods when that action makes sense for your api.

like image 177
elviejo79 Avatar answered Sep 27 '22 22:09

elviejo79