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.

Example:

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 ๐Ÿ™‚

index.php

<?php

// process client request via URL
header(“content-type:application/json”);
include(“functions.php”);
if(!empty($_GET[‘name’]))
{
$name=$_GET[‘name’];
$price = get_price($name);
if(empty($price))
deliver_response(200,”book not found”,NULL);
else
deliver_response(200,”book found”,$price);
}
else
deliver_response(400,”Invalid request”,NULL);

function deliver_response($status,$status_message,$data)
{
header(“HTTP/1.1 $status $status_message”);
$response[‘status’]=$status;
$response[‘status_message’]=$status_message;
$response[‘price’]=$data;
$json_response = json_encode($response);
echo $json_response;
}

?>

functions.php

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

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

?>

.htaccess

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

References:

Advertisements

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.

    1. 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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s