Top Banner
Building Building RESTful Web Service RESTful Web Service With PHP With PHP Huy Nguyen - Huy Nguyen - [email protected]
25

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

Nov 27, 2014

Download

Technology

Nguyen Duc Phu

 
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 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 - [email protected]

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

REST and RESTful web service Building a RESTful framework with PHP

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

REpresentational State Transfer

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

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

What is a RESTful web service?

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

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

It's all about HTTP, dude!

GET POST PUT DELETE HEAD OPTIONS

URIs

MIME types supported

Resources

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

REST vs RPC

RESOURCES vs FUNCTIONS

ROA vs SOA

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

Flickr has a RESTful web service

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

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

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

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

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

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 ...

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

Now let's talk about PHP

... and how it supports REST

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

$_SERVER

Accept => $_SERVER['HTTP_ACCEPT']

Request Method => $_SERVER['REQUEST_METHOD']

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

$_POST and $_GET

Everyone knows them!

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

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" :(

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

Why is symfony not RESTful?

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

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

My RESTful Framework

Creating Resources Creating URIs Handling HTTP methods MIME-type support

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

Resources As Controllers

A resources is a class that handles HTTP methods

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

/** * 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); }

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

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

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

Creating The URIs

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

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

Composite Resources

Konstrukt Framework(konstrukt.dk)

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

MIME Types & Multiple Views

$_SERVER['HTTP_ACCEPT']

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

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

My framework is called My framework is called KaoKao

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

Thank you! I love PHP!

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

Any Question?