25
TAKING CARE OF THE REST Creating your own RESTful API Server Monday, June 20, 2011

Taking Care of The REST - Creating your own RESTful API Server using Restler 2.0

Embed Size (px)

DESCRIPTION

Restler 2.0 (https://github.com/Luracast/Restler) is a single file framework that can host public and protected methods of your PHP class as public and protected api respectively. This presentation talks about the opportunities in todays world and how easy it is to take them using Restler 2.0

Citation preview

Page 1: Taking Care of The REST - Creating your own RESTful API Server using Restler 2.0

TAKING CARE OF THE RESTCreating your own RESTful API Server

Monday, June 20, 2011

Page 2: Taking Care of The REST - Creating your own RESTful API Server using Restler 2.0

WHAT’S HAPPENING?

‣ We are in the Middle of a Revolution

‣ Devices and Tablets are taking over and changing the way we do things

‣ New devices and capabilities are arriving as we speak

Monday, June 20, 2011

Page 3: Taking Care of The REST - Creating your own RESTful API Server using Restler 2.0

WHAT THIS MEANS TO US, DEVELOPERS?

‣ More platforms to Explore

‣ Thus, more opportunities to Monetize

‣ Devices Divide, Developers Rule!

Monday, June 20, 2011

Page 4: Taking Care of The REST - Creating your own RESTful API Server using Restler 2.0

WHAT THIS MEANS TO US, DEVELOPERS?

‣ More platforms to Explore

‣ Thus, more opportunities to Monetize

‣ Devices Divide, Developers Rule!

Monday, June 20, 2011

Page 5: Taking Care of The REST - Creating your own RESTful API Server using Restler 2.0

HOW TO KEEP UP AND EXPAND ASAP?‣ Create your API Server to serve data

and some logic

‣ Create Hybrid Applications if possible‣ HTML5‣ Native (PhoneGap, Custom etc)

‣ Use cross-platform tools‣ Adobe Flash, Flex, and AIR‣ Titanium, Mosync etc

‣ Use least common denominator

Monday, June 20, 2011

Page 6: Taking Care of The REST - Creating your own RESTful API Server using Restler 2.0

WHY TO CREATE YOUR OWN API SERVER?‣ It has the following Advantages• It will serve as the remote model for your

apps• It can hold some business logic for all your

apps• It can be consumed from Web/Mobile/Desktop• It can be exposed to 3rd party developers• It can serve data in different formats to best

suite the device performance

Monday, June 20, 2011

Page 7: Taking Care of The REST - Creating your own RESTful API Server using Restler 2.0

HOW TO CREATE YOUR OWN API SERVER?

‣ Leverage on HTTP protocol

‣ Use REST (Representational State Transfer)

‣ Use different formats• Json (JavaScript Object Notation)• XML (eXtended Markup Language)• Plist (XML/Binary Property List)• Amf (Action Messaging Format)

Monday, June 20, 2011

Page 8: Taking Care of The REST - Creating your own RESTful API Server using Restler 2.0

INTRODUCING LURACAST RESTLER‣ Easy to use RESTful API Server

‣ Light weight Micro-framework

‣ Supports many formats with two way auto conversion

‣ Written in PHP

‣ Free

‣ Open source (LGPL)

Monday, June 20, 2011

Page 9: Taking Care of The REST - Creating your own RESTful API Server using Restler 2.0

IF YOU KNOW OBJECT ORIENTED PHP

‣ You already know how to use RESTLER

‣ Its that simple

Monday, June 20, 2011

Page 10: Taking Care of The REST - Creating your own RESTful API Server using Restler 2.0

GETTING STARTEDCreating a hello world example in 3 steps

Monday, June 20, 2011

Page 11: Taking Care of The REST - Creating your own RESTful API Server using Restler 2.0

STEP-1 CREATE YOUR CLASS

‣ Create a class and define its methods

‣ Save it in root of your web site and name it in lower case with .php extension

<?phpclass Simple {! function index() {! ! return 'Hello World!';! }! function sum($n1, $n2) {! ! return $n1+$n2;! }}

simple.php

Monday, June 20, 2011

Page 12: Taking Care of The REST - Creating your own RESTful API Server using Restler 2.0

STEP-2 COPY RESTLER

‣ Copy restler.php to the same web root

‣ It contains all the needed classes and interfaces

restler.php

Monday, June 20, 2011

Page 13: Taking Care of The REST - Creating your own RESTful API Server using Restler 2.0

STEP-3 CREATE GATEWAY

‣ Create index.php

‣ Create an instance of Restler class

<?phpspl_autoload_register();$r = new Restler();$r->addAPIClass('Simple');$r->handle();

index.php

‣ Add Simple as the API class

‣ Call the handle() method

Monday, June 20, 2011

Page 14: Taking Care of The REST - Creating your own RESTful API Server using Restler 2.0

CONSUMING OUR APIUnderstanding how url mapping works

Monday, June 20, 2011

Page 15: Taking Care of The REST - Creating your own RESTful API Server using Restler 2.0

URL MAPPING

base_path/{gateway}/{class_name}base_path/index.php/simple

mapped to the result of index() method in Simple class

Monday, June 20, 2011

Page 16: Taking Care of The REST - Creating your own RESTful API Server using Restler 2.0

URL MAPPING

base_path/{gateway}/{class_name}/{method_name}base_path/index.php/simple/sum

mapped to the result of sum() method in Simple class

Monday, June 20, 2011

Page 17: Taking Care of The REST - Creating your own RESTful API Server using Restler 2.0

ADVANCED URL MAPPING‣ Use .htaccess file to remove

the index.php from the url (http://rest.ler/simple/sum)

‣ Pass an empty string as the second parameter for addAPIClass() method (http://rest.ler/sum)

‣ These are just conventions, we can use a javadoc style comment to configure (http://rest.ler/math/add)

DirectoryIndex index.php<IfModule mod_rewrite.c>! RewriteEngine On! RewriteRule ^$ index.php [QSA,L]! RewriteCond %{REQUEST_FILENAME} !-f! RewriteCond %{REQUEST_FILENAME} !-d! RewriteRule ^(.*)$ index.php [QSA,L]</IfModule>

<?phpspl_autoload_register();$r = new Restler();$r->addAPIClass('Simple','');$r->handle();

/*** @url GET math/add*/function sum($n1, $n2) {! return $n1+$n2;}

Monday, June 20, 2011

Page 18: Taking Care of The REST - Creating your own RESTful API Server using Restler 2.0

PASSING PARAMETERS

.../{class_name}/{param1}/{param2}.../simple/4/5

parameters are passed in order as url itself

Monday, June 20, 2011

Page 19: Taking Care of The REST - Creating your own RESTful API Server using Restler 2.0

PASSING PARAMETERS

.../{class_name}?param1=value&param2=value.../simple?n1=4&n2=5

named parameters passed as query string

Monday, June 20, 2011

Page 20: Taking Care of The REST - Creating your own RESTful API Server using Restler 2.0

RETURNING COMPLEX DATA‣ Restler can handle all the

following PHP types

✓Number

✓Boolean (True | False)

✓String

✓Indexed/Associative array

✓Objects

<?phpclass Simple {! function index() {! ! return 'Hello World!';! }! function sum($n1, $n2) {! ! return array('sum'=>$n1+$n2);! }}

Monday, June 20, 2011

Page 21: Taking Care of The REST - Creating your own RESTful API Server using Restler 2.0

SUPPORTING FORMATS‣ Step1 - Copy the format file(s)

‣ Step2 - Call setSupportedFormats() method and pass the formats

‣ Restler supports the following formats- JSON- XML- Plist- AMF- YAML

<?php$r = new Restler();$r->addAPIClass('Simple','');$r->setSupportedFormats( 'XmlFormat','JsonFormat');$r->handle();

Monday, June 20, 2011

Page 22: Taking Care of The REST - Creating your own RESTful API Server using Restler 2.0

PROTECTING SOME OF THE API

‣ Step1 - Create an Auth Class implementing iAuthenicate interface

‣ Step2 - Call addAuthenticationClass() method and pass that class

‣ Simply change the methods to be protected as protected methods

<?php$r = new Restler();$r->addAPIClass('Simple','');$r->setSupportedFormats(

'XmlFormat','JsonFormat');$r->addAuthenticationClass('SimpleAuth');$r->handle();

protected function sum($n1, $n2) {! return array('sum'=>$n1+$n2);}

<?phpclass SimpleAuth implements iAuthenticate{! function __isAuthenticated() {! ! return $_GET['token']=='178261dsjdkjho8f' ? TRUE : FALSE;! }}

Monday, June 20, 2011

Page 23: Taking Care of The REST - Creating your own RESTful API Server using Restler 2.0

USING HTTP METHODS & CRUD

‣ GET - Retrieve

‣ POST - Create

‣ PUT - Update

‣ DELETE - Delete

<?phpclass User {! $dp = new DataProvider('User');! function get() {! ! return $this->dp->getAll();! }! function post($data) {! ! return $this->dp->create($data);! }! function put($idx, $data) {! ! return $this->dp->update($idx, $data);! }! function delete($idx, $data) {! ! return $this->dp->delete($idx, $data);! }}

Monday, June 20, 2011

Page 24: Taking Care of The REST - Creating your own RESTful API Server using Restler 2.0

REAL WORLD RESTLER EXAMPLE

Monday, June 20, 2011

Page 25: Taking Care of The REST - Creating your own RESTful API Server using Restler 2.0

ABOUT LURACAST

Monday, June 20, 2011