1. Bangalore PHP User Group – 31 October 2009
Building RESTful Applications Using
PHP
Sudheer Satyanarayana
http://techchorus.net
http://binaryvibes.co.in
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
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. 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
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. 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. 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. 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. 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. Constraint 5 : Layered System
Proxies, gateways and other intermediaries
Encapsulate legacy services
Protect new services from legacy clients
Load balancing
High performance
19. Constraint 6: Code On Demand (Optional)
Client downloads code from server and
executes it
Common example: Server generating
JavaScript code
20. If your system conforms to these constraints it
can be called
RESTful
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);
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. Use Apache's mod_rewrite to make URLs pretty
Other web servers offer similar functionality
$_SERVER['REQUEST_URI']
27. Encode data to and decode from JSON
json_encode()
json_decode()
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/