tags:

views:

506

answers:

3

Hi guys. I'm developing php library. So i want to add Rest to my library.

How to write Rest in PHP ? Any idea?

Thank you for every advise?

+5  A: 

You may want to look at this article and the follow-up: http://www.gen-x-design.com/archives/create-a-rest-api-with-php/

Your question is very open-ended, so this tutorial may be the best starting point.

James Black
A: 

Can't hurt to go back to the original source of the term REST, and be sure that you understand what that means.

Sixten Otto
+2  A: 

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 dose 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.

elviejo
-1: Sorry, no language is REST by default. For instance in REST a POST request is very different than GET, and you need to support DELETE and PUT as well.
Jim Ferrans
@Jim Ferrans - Why must DELETE be supported?
James Black
@elviejo - If you look at the urls for stackoverflow you will see a REST url. This is not something that PHP does by default, in the parsing, as there is no question mark.
James Black
@James Black the fact that StackOverflow makes pretty url rewrites doesn't make it more or less REST.so.com/questions/1628628is a valid REST call as is this:so.com/questions.php?id=1628628Both calls are using the get method of the http protocol.
elviejo
@Jim Ferrans: Added some clarifications in my answer trying to address your concerns.
elviejo