Successfully reported this slideshow.

Building Restful Applications Using Php

8,518 views

Published on

Slides presented at Bangalore PHP User Group Meeting - October 31 2009 by Sudheer Satyanarayana.

Published in: Technology

Building Restful Applications Using Php

  1. 1. Bangalore PHP User Group – 31 October 2009 Building RESTful Applications Using PHP Sudheer Satyanarayana http://techchorus.net http://binaryvibes.co.in
  2. 2. About Your Presenter Director, Binary Vibes Information Technologies Pvt. Ltd (http://binaryvibes.co.in) Free and open source software supporter PHP programmer Blogger - http://techchorus.net
  3. 3. Topics What is REST? Why REST? How to use REST?
  4. 4. What Is REST? Representational State Transfer Style of software architecture Introduced by Roy Fielding
  5. 5. Six Constraints Describe The Architectural Style Client Server Statelessness Cacheable Uniform interface Layered system Code on demand
  6. 6. Constraint 1: Separation Of Clients And Servers
  7. 7. Clients Browser PHP/Python/Perl/Whatever script Desktop application Command line Anything that can make an HTTP request
  8. 8. Advantages Of Separation Of Clients And Servers Portability: clients are not bothered about data storage Scalibility: servers are not bothered about user interface Independent development of clients and servers
  9. 9. Constraint 2: Statelessness HTTP is stateless Server does not store client context Clients may hold context Each request contains all information required to process the request
  10. 10. Advantages Of Statelessness Servers are more scalable Servers are more reliable Servers are more visible for monitoring
  11. 11. Constraint 3 : Cacheable Responses indicate whether they are cacheable Eliminates some client server interactions
  12. 12. Advantages Of Cacheability Performance Scalability
  13. 13. Constraint 4: Uniform Interface REST is defined by four interface constraints Identification of resources Manipulation of resources through representations Self descriptive messages Hypermedia as the engine of application state
  14. 14. Identification Of Resources Resource can be anything Article Comment Contact User Employee URI – Uniform Resource Identifier http://example.com/article/1 http://example.com/comment http://example.com/article?title=abc
  15. 15. Manipulation Of Resrources Through Representations Use the HTTP verbs GET – retrieve one or a collection of articles POST – create a resource PUT – modify an article DELETE – delete an article http://example.com/article GET Return collection of articles http://example.com/article/1 GET Return article whose id is 1 http://example.com/article POST Create an article http://example.com/article/1 PUT Modify the article whose id is 1 http://example.com/article/1 DELETE Delete the article whose id is 1
  16. 16. Self Descriptive Messages HTTP Headers HTTP Response Codes 1XX Informational 2XX Success 200 Ok 201 Created 202 Accepted 204 No Content 3XX Redirection 4XX Client Error 403 Forbidden 404 Not Found 5XX Server Error
  17. 17. Hypermedia As The Engine Of Application State Provide sufficient information to access related resources When http://example.com/article/1 is accessed Inform the client how to access the comments of article 1 Mention http://example.com/comments?criteria=byarticle&articleid=1 in the response
  18. 18. Constraint 5 : Layered System Proxies, gateways and other intermediaries Encapsulate legacy services Protect new services from legacy clients Load balancing High performance
  19. 19. Constraint 6: Code On Demand (Optional) Client downloads code from server and executes it Common example: Server generating JavaScript code
  20. 20. If your system conforms to these constraints it can be called RESTful
  21. 21. REST Advantages Performance Scalability Simplicity Modifiability Visibility Portability Reliability
  22. 22. Tips to put these concepts into practice
  23. 23. PHP header() function Use header() function to describe the response headers header("HTTP/1.0 404 Not Found"); header(”Location: http://example.com/movedhere”); header('Content-type: application/xml); header('Content-type: application/json);
  24. 24. Determine the request method $_SERVER['REQUEST_METHOD']
  25. 25. Use cUrl from the CLI to test REST server Write a PHP script to make HTTP requests PHP supports curl, use the PHP curl_* functions Streams and sockets functions are also available Telnet also works
  26. 26. Use Apache's mod_rewrite to make URLs pretty Other web servers offer similar functionality $_SERVER['REQUEST_URI']
  27. 27. Encode data to and decode from JSON json_encode() json_decode()
  28. 28. Authentication HTTP Authentication API Key in HTTP header apache_request_headers() $_SERVER
  29. 29. Your favourite PHP framework might have a component to help develop RESTful servers Use it
  30. 30. Questions?
  31. 31. Contact me http://twitter.com/bngsudheer http://techchorus.net IRC, Bonaparte on freenode.net Thank you
  32. 32. Copyright Notice This presentation is licensed under the Creative Commons Attribution- Noncommercial-Share Alike 2.5 India http://creativecommons.org/licenses/by-nc-sa/2.5/in/

×