Skip to content


Writing a REST Service: How to write Rest Client and Servers

What is the REST (Representational State Transfer) :

REST is a style of software architecture for distributed hypermedia systems such as the World Wide Web. The terms “representational state transfer” and “REST” were introduced and defined in 2000 by the doctoral dissertation of Roy Fielding, It uses Hypertext Transfer Protocol (HTTP) specification versions 1.0 and 1.1.

Systems which follow REST principles are often referred to as “RESTful”.(http://en.wikipedia.org/wiki/Representational_State_Transfer)

REST and XML-RPC Differencess:
Most general difference  XML-RPC very structural but REST is not , creating a web service with REST more filexible.
Look at below the examples.

REST Request
GET /rest_server.php?function=cmd HTTP/1.1
or
POST (Raw post data)

REST uses HTTP and POST, PUT and DELETE. GET methods.
Making request with REST is very bare.

REST Response
HTTP/1.1 200 OK

<?xml version=”1.0″ encoding=”UTF-8″ ?>
- <rest>
- <example.test1>
- <cities>
<city_id>1</city_id>
<name>istanbul</name>
</cities>
- <cities>
<city_id>2</city_id>
<name>nyc</name>
</cities>
- <cities>
</rest>

REST response can be very easy unlike XML-RPC response.You can change the general xml structure of the rest response.REST services generally uses xml_serizalizer for writing xml content with more filexible options.

XML-RPC Request

POST /example HTTP/1.1
Host: www.develturk.com

 <?xml version=”1.0″?>
<methodCall>
<methodName>example.test1</methodName>
<params>
<param>
<value><string>Blabalblabla</string></value>
</param>
</params>
</methodCall>

The XML-RPC request is the most verbose of all the protocols. This verbosity is what makes XML-RPC both difficult to use.Using the xml-rpc services requires some tricky.

XML-RPC Response

HTTP/1.1 200 OK

<?xml version=”1.0″?>
<methodCall>
<methodName>example.test1</methodName>
<params>
<param>
<value><double>36.5</double></value>
</param>
</params>
</methodCall>

Like XML-RPC similars services uses structural data format for transfering data (sending and getting data) but transporting data with REST you don’t have to provide structural data format so i think REST services more customizable than others.

HTTP Methods for REST

Method CRUD Operation Description
GET Retrieve Retrieves the representation of a
resource.
HEAD   Retrieves metadata for the
representation and resource.
POST Create In the strict sense, POST creates a resource. In the real world,however, POST
is typically used to create, update, and even delete a resource. It is normal to use REST services that support only GET and POST.
PUT Update Updates a resource. More often than not, you will not see this method used in the real world but instead will see POST used to perform the actions.
DELETE Delete Deletes a resource. Just like PUT, in the real world this is rarely used, and instead POST is used in its place


Requirements for building a REST Server:

I use PEAR libraries because of you need a lot of library for writing a REST Service.A rest library contains two separate class Client and Server.Client class can make request using PEAR HTTP Request Class or php CURL.

Server class will produce xml content with PEAR::xml_serializer class.After that making request process you will get response (xml content) with HTTP Request Class and then you can convert xml content to array with PEAR::xml_unserializer.

So follow this steps:

 1 - Write a handle for client and server process
2 - Make http request with a class like PEAR::http_request
3 - Return response with xml_serializer class.
4 - Parse response with xml_unserializer class and return to array.

I wrote a rest framework for this job.It name is Easyrest.
Look at my next article !

  • Facebook
  • Digg
  • Delicious
  • Google Bookmarks
  • Technorati Favorites
  • Yahoo Bookmarks
  • Webnews
  • Technotizie
  • Taggly
  • Linkatopia
  • Ping
  • StumbleUpon
  • Twitter
  • Share/Bookmark

Posted in php articles.

Tagged with , , , , , , , , , .


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.



Easy AdSense by Unreal