intoduction to php restful web service

26
REST Web Services

Upload: baabtracom-mentoring-partner-first-programming-school-in-india

Post on 05-Dec-2014

1.161 views

Category:

Technology


10 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Intoduction to php  restful web service

REST –Web Services

Page 2: Intoduction to php  restful web service

Web Basics: Retrieving Information using HTTP

GET

• The user types in at his browser: http://www.amazon.com

• - The browser software creates an HTTP header (no payload)

• - The HTTP header identifies:

• - The desired action: GET ("get me resource")

• - The target machine (www.amazon.com)

AmazonWeb Server

http://www.amazon.com

GET / HTTP/1.1Host: http://www.amazon.com

Page 3: Intoduction to php  restful web service

Web Basics: Updating Information using HTTP

POST

• The user fills in the Web page's form

• The browser software creates an HTTP header with a payload

comprised of the form data. The HTTP header identifies:

The desired action: POST ("here's some update info")

The target machine (amazon.com)

The payload contains: The data being POSTed

Credit Card: Visa

Number: 123-45-6789

Expiry: 12-04-06

Credit Card: VisaNumber: 123-45-6789Expiry: 12-04-06

AmazonWeb Server

Page 4: Intoduction to php  restful web service

What is REST?

• RESET stands for Representational state transfer

•" REST " was coined by Roy Fielding in his Ph.D. to describe a

design pattern for implementing networked systems.

• In the REST architectural style, data and functionality are

considered resources and are accessed usingUniform Resource

Identifiers (URIs), typically links on the Web.

Page 5: Intoduction to php  restful web service

Creating RESTful Web Service

Page 6: Intoduction to php  restful web service

Learn by example

• We are going to create a web service for a library -so that it will manage

request from clients and will respond with the details of the book

requested

• We will have the following files in our project (in www/library/)

– Functions.php // has an array of books and will return the price of book requested

– Index.php // will handle requests from clients and respond with data

– Request.php // will act as the client who request the data

Page 7: Intoduction to php  restful web service

<?php /* functions.php */

function get_price($find) {

$books = array('java'=>300,

'php'=>200,

'c'=>100);

foreach($books as $book=>$price) {

if($book==$find)

{

return $price;

break;

}

}

}

?>

Functions.php

• We have created a simple php

page named “function.php”

• Inside the page we have defined

a function named “get_price()”

that will accept book name as

argument and will return with its

price

Page 8: Intoduction to php  restful web service

Index.php

<?php

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);

}

Page 9: Intoduction to php  restful web service

Creating RESTful Web Service

<?php

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);

}

Created a header mentioning the response will be in json format

Page 10: Intoduction to php  restful web service

Creating RESTful Web Service

<?php

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);

}

Including the page functions.php that we have created earlier

Page 11: Intoduction to php  restful web service

Creating RESTful Web Service

<?php

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);

}

Checking whether the URL request contains the name of book or not?

If the URL request doesn’t have the book name returns error message

Page 12: Intoduction to php  restful web service

Creating RESTful Web Service

<?php

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);

}

Accepting the argument into a variable named “$name”

Page 13: Intoduction to php  restful web service

Creating RESTful Web Service

<?php

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);

}

Calling the function get_price() with the book name and stores the price returned in a varaible named $price

Page 14: Intoduction to php  restful web service

Creating RESTful Web Service

<?php

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);

}

If the price is empty – calls a function named deliver_responsewith the “book not found” arguments

Page 15: Intoduction to php  restful web service

Creating RESTful Web Service

<?php

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);

}

If the price is not empty – call the same method deliver_responsewith “book found ” arguments

Page 16: Intoduction to php  restful web service

Creating RESTful Web Service

function deliver_response($status,$status_message,$data)

{

header("HTTP/1.1 $status $status_message");

$response['status']=$status;

$response['status_message']=$status_message;

$response['data']=$data;

$json_response = json_encode($response);

echo $json_response;

}

?>

Sets the header witht the status code and message

Page 17: Intoduction to php  restful web service

Creating RESTful Web Service

function deliver_response($status,$status_message,$data)

{

header("HTTP/1.1 $status $status_message");

$response['status']=$status;

$response['status_message']=$status_message;

$response['data']=$data;

$json_response = json_encode($response);

echo $json_response;

}

?>

Creates an array of status, messafe and the data(ieprice of book)

Page 18: Intoduction to php  restful web service

Creating RESTful Web Service

function deliver_response($status,$status_message,$data)

{

header("HTTP/1.1 $status $status_message");

$response['status']=$status;

$response['status_message']=$status_message;

$response['data']=$data;

$json_response = json_encode($response);

echo $json_response;

}

?>

Create a json object using an inbuilt function in phpnamed json_encode() and passing the array we just created as argument

Page 19: Intoduction to php  restful web service

Creating RESTful Web Service

function deliver_response($status,$status_message,$data)

{

header("HTTP/1.1 $status $status_message");

$response['status']=$status;

$response['status_message']=$status_message;

$response['data']=$data;

$json_response = json_encode($response);

echo $json_response;

}

?>

Echo the json object so that any clients request the book will be get the details as a json object

Page 20: Intoduction to php  restful web service

How to call a Web Service – request.php

<?php

$url= 'http://127.1.1.0/library/?name=php';

$response = file_get_contents($url);

$data = json_decode($response, true);

var_dump($data);

?>

Creates a url to which we pass an argument name=php

Page 21: Intoduction to php  restful web service

How to call a Web Service – request.php

<?php

$url= 'http://127.1.1.0/library/?name=php';

$response = file_get_contents($url);

$data = json_decode($response, true);

var_dump($data);

?>

Makes the request with the url we created and stores the result in a variable named $response(obviously the result will be returned in jsonformat)

Page 22: Intoduction to php  restful web service

How to call a Web Service – request.php

<?php

$url= 'http://127.1.1.0/library/?name=php';

$response = file_get_contents($url);

$data = json_decode($response, true);

var_dump($data);

?>

Decodes the json result into array format and displays it on the screen

Page 23: Intoduction to php  restful web service

Questions?

“A good question deserve a good grade…”

Page 24: Intoduction to php  restful web service

Self Check !!

Page 25: Intoduction to php  restful web service

If this presentation helped you, please visit our page facebook.com/baabtra and like it.

Thanks in advance.

www.baabtra.com | www.massbaab.com |www.baabte.com

Page 26: Intoduction to php  restful web service

Contact Us

Emarald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

Start up VillageEranakulam,Kerala, India.

Email: [email protected]