Building Restful Applications Using Php

Post on 10-May-2015

7.548 views 5 download

Tags:

description

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

Transcript of 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

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

What is REST?

Why REST?

How to use REST?

Topics

What Is REST?

Representational State Transfer

Style of software architecture

Introduced by Roy Fielding

Six Constraints Describe The Architectural Style

Client Server

Statelessness

Cacheable

Uniform interface

Layered system

Code on demand

Constraint 1: Separation Of Clients And Servers

Clients

Browser

PHP/Python/Perl/Whatever script

Desktop application

Command line

Anything that can make an HTTP request

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

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

Advantages Of Statelessness

Servers are more scalable

Servers are more reliable

Servers are more visible for monitoring

Constraint 3 : Cacheable

Responses indicate whether they are cacheable

Eliminates some client server interactions

Advantages Of Cacheability

Performance

Scalability

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

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

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

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

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

Constraint 5 : Layered System

Proxies, gateways and other intermediaries

Encapsulate legacy services

Protect new services from legacy clients

Load balancing

High performance

Constraint 6: Code On Demand (Optional)

Client downloads code from server and executes it

Common example: Server generating JavaScript code

If your system conforms to these constraints it can be called

RESTful

REST Advantages Performance

Scalability

Simplicity

Modifiability

Visibility

Portability

Reliability

Tips to put these concepts into practice

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

Determine the request method

$_SERVER['REQUEST_METHOD']

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

Use Apache's mod_rewrite to make URLs pretty

Other web servers offer similar functionality

$_SERVER['REQUEST_URI']

Encode data to and decode from JSON

json_encode()json_decode()

Authentication

HTTP Authentication

API Key in HTTP header apache_request_headers() $_SERVER

Authentication

HTTP Authentication

API Key in HTTP header

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

Use it

Questions?

Contact me

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

Thank you

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/