32
Building RESTful Applications Using PHP Sudheer Satyanarayana http://techchorus.net http://binaryvibes.co.in Bangalore PHP User Group – 31 October 2009

Building Restful Applications Using Php

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: Building Restful Applications Using Php

Building RESTful Applications Using PHP

Sudheer Satyanarayana http://techchorus.net http://binaryvibes.co.in

Bangalore PHP User Group – 31 October 2009

Page 2: Building Restful Applications Using Php

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

Page 3: Building Restful Applications Using Php

What is REST?

Why REST?

How to use REST?

Topics

Page 4: Building Restful Applications Using Php

What Is REST?

Representational State Transfer

Style of software architecture

Introduced by Roy Fielding

Page 5: Building Restful Applications Using Php

Six Constraints Describe The Architectural Style

Client Server

Statelessness

Cacheable

Uniform interface

Layered system

Code on demand

Page 6: Building Restful Applications Using Php

Constraint 1: Separation Of Clients And Servers

Page 7: Building Restful Applications Using Php

Clients

Browser

PHP/Python/Perl/Whatever script

Desktop application

Command line

Anything that can make an HTTP request

Page 8: Building Restful Applications Using Php

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

Page 9: Building Restful Applications Using Php

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

Page 10: Building Restful Applications Using Php

Advantages Of Statelessness

Servers are more scalable

Servers are more reliable

Servers are more visible for monitoring

Page 11: Building Restful Applications Using Php

Constraint 3 : Cacheable

Responses indicate whether they are cacheable

Eliminates some client server interactions

Page 12: Building Restful Applications Using Php

Advantages Of Cacheability

Performance

Scalability

Page 13: Building Restful Applications Using Php

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

Page 14: Building Restful Applications Using Php

Identification Of Resources

Resource can be anything Article Comment Contact User Employee

http://example.com/article/1http://example.com/commenthttp://example.com/article?title=abc

URI – Uniform Resource Identifier

Page 15: Building Restful Applications Using Php

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

Page 16: Building Restful Applications Using Php

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

Page 17: Building Restful Applications Using Php

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

Page 18: Building Restful Applications Using Php

Constraint 5 : Layered System

Proxies, gateways and other intermediaries

Encapsulate legacy services

Protect new services from legacy clients

Load balancing

High performance

Page 19: Building Restful Applications Using Php

Constraint 6: Code On Demand (Optional)

Client downloads code from server and executes it

Common example: Server generating JavaScript code

Page 20: Building Restful Applications Using Php

If your system conforms to these constraints it can be called

RESTful

Page 21: Building Restful Applications Using Php

REST Advantages Performance

Scalability

Simplicity

Modifiability

Visibility

Portability

Reliability

Page 22: Building Restful Applications Using Php

Tips to put these concepts into practice

Page 23: Building Restful Applications Using Php

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

Page 24: Building Restful Applications Using Php

Determine the request method

$_SERVER['REQUEST_METHOD']

Page 25: Building Restful Applications Using Php

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

Page 26: Building Restful Applications Using Php

Use Apache's mod_rewrite to make URLs pretty

Other web servers offer similar functionality

$_SERVER['REQUEST_URI']

Page 27: Building Restful Applications Using Php

Encode data to and decode from JSON

json_encode()json_decode()

Page 28: Building Restful Applications Using Php

Authentication

HTTP Authentication

API Key in HTTP header apache_request_headers() $_SERVER

Authentication

HTTP Authentication

API Key in HTTP header

Page 29: Building Restful Applications Using Php

Your favourite PHP framework might have a component to help develop RESTful servers

Use it

Page 30: Building Restful Applications Using Php

Questions?

Page 31: Building Restful Applications Using Php

Contact me

http://twitter.com/bngsudheer http://techchorus.net IRC, Bonaparte on freenode.net

Thank you

Page 32: Building Restful Applications Using Php

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/