Intoduction to php restful web service

1,807 views

Published on

Published in: Technology
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,807
On SlideShare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
83
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

Intoduction to php restful web service

  1. 1. REST –Web Services
  2. 2. 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
  3. 3. 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
  4. 4. 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.
  5. 5. Creating RESTful Web Service
  6. 6. 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
  7. 7. <?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
  8. 8. 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); }
  9. 9. 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
  10. 10. 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
  11. 11. 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
  12. 12. 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”
  13. 13. 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
  14. 14. 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
  15. 15. 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
  16. 16. 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
  17. 17. 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)
  18. 18. 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
  19. 19. 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
  20. 20. 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
  21. 21. 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)
  22. 22. 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
  23. 23. Questions? “A good question deserve a good grade…”
  24. 24. Self Check !!
  25. 25. 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
  26. 26. 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

×