Hanoi php day 2008 - 05. nguyen hai nhat huy - building-restful-web-service-with-php

Post on 27-Nov-2014

1.331 views 4 download

Tags:

description

 

Transcript of Hanoi php day 2008 - 05. nguyen hai nhat huy - building-restful-web-service-with-php

BuildingBuildingRESTful Web Service RESTful Web Service With PHPWith PHPHuy Nguyen - Huy Nguyen - huy@byhuy.com

REST and RESTful web service Building a RESTful framework with PHP

REpresentational State Transfer

An architecture not a protocol or a standard Resource Oriented Approach World Wide Web is the key example

What is a RESTful web service?

A web site for Machines!!!A web site for Machines!!!

It's all about HTTP, dude!

GET POST PUT DELETE HEAD OPTIONS

URIs

MIME types supported

Resources

REST vs RPC

RESOURCES vs FUNCTIONS

ROA vs SOA

Flickr has a RESTful web service

But it's crap!!!!!!!

This is RESTful: http://feeds.feedburner.com/Geshansblog

and this is not: http://api.flickr.com/services/rest/?method=flickr.test.echo&name=value

RESTful Web Protocols

Syndication protocols (RSS, Atom Feed) Atom Publishing Protocol RDF and OWL OpenID Single Sign-on Standard Google's OpenSocial Protocol Amazon's S3 Protocol ...

Now let's talk about PHP

... and how it supports REST

$_SERVER

Accept => $_SERVER['HTTP_ACCEPT']

Request Method => $_SERVER['REQUEST_METHOD']

$_POST and $_GET

Everyone knows them!

RAW POST/PUT REQUEST DATA?

<?php

// get RAW HTTP POST$rawData = file_get_contents('php://input');

// but this won't work with enctype="multipart/form-data" :(

Why is symfony not RESTful?

because RESTful web service is about resources, not functions/actions

My RESTful Framework

Creating Resources Creating URIs Handling HTTP methods MIME-type support

Resources As Controllers

A resources is a class that handles HTTP methods

/** * HTTP GET handler */ public function __handleGet() { // Not implemented $this->_response->setResponseCode(501); } /** * HTTP POST handler */ public function __handlePost() { // Not implemented $this->_response->setResponseCode(501); } /** * HTTP PUT handler */ public function __handlePut() { // Not implemented $this->_response->setResponseCode(501); }

/** * HTTP DELETE handler */ public function __handleDelete() { // Not implemented $this->_response->setResponseCode(501); } /** * HTTP HEAD handler */ public function __handleHead() { // Not implemented $this->_response->setResponseCode(501); }

Creating The URIs

/blog/([a-zA-Z0-9]+)/? => blogResource

Composite Resources

Konstrukt Framework(konstrukt.dk)

MIME Types & Multiple Views

$_SERVER['HTTP_ACCEPT']

text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

My framework is called My framework is called KaoKao

Thank you! I love PHP!

Any Question?