Intoduction to php  restful web service
Upcoming SlideShare
Loading in...5
×
 

Intoduction to php restful web service

on

  • 412 views

 

Statistics

Views

Total Views
412
Views on SlideShare
410
Embed Views
2

Actions

Likes
1
Downloads
20
Comments
0

1 Embed 2

http://manage.baabtra.com 2

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Intoduction to php  restful web service Intoduction to php restful web service Presentation Transcript

  • REST –Web Services
  • 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) Amazon Web Server http://www.amazon.com GET / HTTP/1.1 Host: http://www.amazon.com
  • 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: Visa Number: 123-45-6789 Expiry: 12-04-06 Amazon Web Server
  • 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.
  • Creating 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
  • <?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
  • 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); }
  • 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
  • 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
  • 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
  • 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”
  • 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
  • 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_response with the “book not found” arguments
  • 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_response with “book found ” arguments
  • 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
  • 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(ie price of book)
  • 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 php named json_encode() and passing the array we just created as argument
  • 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
  • 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
  • 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 json format)
  • 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
  • Questions? “A good question deserve a good grade…”
  • Self Check !!
  • 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
  • Contact Us Emarald Mall (Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Start up Village Eranakulam, Kerala, India. Email: info@baabtra.com