Basic example on RESTful webservices in php

Hope you are aware on basics of RESTful webservices in php..if not pls clickhere

Lets start up with a basic example on RESTful services. I would like to thank YRRHELP in youtube for providing best example on this..

First let me explain a basic difference between website and webservices. I too have the same doubt before i review YRR tutorial..he explained in detail..let me start with that..

Difference between Website & Webservices:

Website will be requested by humans and response will show in HTML

Webservices: In most of the cases, request will be done by programs and response will be generated in XML or Json

This webservices is widely used to communicate between heterogeneous system and platform like PHP,Windows and anyother platforms can communicate to java/android.


Lets start up with a basic example like, if you call up for a book(C/Java) in an URL should display its price in JSON format.

02-04-2014 19-50-59

From the above pic you can notice that if i type “”C” it should display its price..similarly if i type “java” it should display java book price..

Here everything is coded in program and if you type some parameter it will display that desired result. This how webservices works..replace book name with city name and price with temperature will display the temperature of the city. This is the best real world example which i can explain.

Now lets peek into the code.. 😉

1. create a folder in wamp server with name rest

2. create a index.php file

3. create a functions.php file

4. create a htaccess file

Now just copy the code from below..then save and run 🙂



// process client request via URL
$price = get_price($name);
deliver_response(200,”book not found”,NULL);
deliver_response(200,”book found”,$price);
deliver_response(400,”Invalid request”,NULL);

function deliver_response($status,$status_message,$data)
header(“HTTP/1.1 $status $status_message”);
$json_response = json_encode($response);
echo $json_response;



function get_price($find)
$books = array(“java”=>299,”c”=>348,”php”=>267);

foreach($books as $book=>$price)
if($book == $find)
return $price;



options +FollowSymlinks
RewriteEngine On

RewriteRule ^([a-zA-Z-]*)$ index.php?name=$1 [nc,qsa]

I hope by this time your brain is running with a doubt where comes this htaccess to live?? good question..let me explain with an image..

It will be very difficult to call up with parameters like below…

02-04-2014 19-50-59

Hence by using htaccess we can do like below..

02-04-2014 19-59-17


clickhere to know more about htaccess



2 thoughts on “Basic example on RESTful webservices in php

  1. Hmm it seems like your blog ate my first comment (it was super long) so
    I guess I’ll just sum it up what I had written and say, I’m
    thoroughly enjoying your blog. I too am an aspiring blog blogger but I’m still new to everything.
    Do you have any tips for rookie blog writers? I’d really appreciate it.

    • hi,
      thnx fr ur comments :). Myself also very new to blogging..i always try to make it simple for visitors to understand the concept. Let me know some tips if u have any ;). Share your blog url to me, so that i can learn something from your blog 😀

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s