Professional Documents
Culture Documents
Restful Api Design & Implementation With Codeigniter PHP Framework
Restful Api Design & Implementation With Codeigniter PHP Framework
WhoAmI
Bo-Yi Wu
@appleboy
http://blog.wu-boy.com
RealTek(IC Design House)
- TV
- Sencha Touch 2, Backbone.js, CodeIgniter,
Node.js, MongoDB, MySQL, Twitter Bootstrap,
Twitter Hogan ...
2012 PHPConf
Who Am I
CodeIgniter-Native-Session
CodeIgniter-i18n
CodeIgniter-Template
CodeIgniter-Nexmo-Message
CodeIgniter-TextMagic-API
2012 PHPConf
My Focus
Support HMVC
2012 PHPConf
Outline
Restful API Basic
API Design
Implementing API with CodeIgniter
Verify your API
2012 PHPConf
Database
2012 PHPConf
(MySQL,MongoDB)
POST
GET
PUT
DELETE
OPTIONS
2012 PHPConf
PUT, DELETE
http://api.jquery.com/jQuery.ajax/
2012 PHPConf
2012 PHPConf
10
2012 PHPConf
11
2012 PHPConf
12
JSON*
XML
Array
Html
CSV
2012 PHPConf
13
JSON
, ,
2012 PHPConf
14
{
key1: 'value1',
key2: 20121103
key3: [1,2,3]
}
2012 PHPConf
15
var object = {
key1: 'value1',
key2: 20121103
key3: [1,2,3]
}
2012 PHPConf
16
PHP
Outputs
<?php
echo json_encode(array(
'key' => 'value'
));
{key: 'value'}
2012 PHPConf
17
PHP
Outputs
array(
'key' => 'value'
);
2012 PHPConf
18
JSON
http://goo.gl/Wvhwb
2012 PHPConf
19
API Design
2012 PHPConf
20
API
Simple
Intuitive
Stable
Well Document
2012 PHPConf
21
2012 PHPConf
22
2012 PHPConf
23
API
2012 PHPConf
24
Debug
2012 PHPConf
25
2012 PHPConf
26
Http Method
RFC 2616
CRUD
Method
Create
POST
Read
GET
Update
PUT
Delete
DELETE
2012 PHPConf
27
28
/API/Module/Method
2012 PHPConf
29
/API/Topic/Add
/API/Topic/Update
/API/Topic/Delete
/API/Topic/List
2012 PHPConf
30
/API/Topic/Add
/API/Topic/Update/1234
/API/Topic/Delete/1234
/API/Topic/List/sort/asc
2012 PHPConf
31
1
2012 PHPConf
32
API
2012 PHPConf
33
2012 PHPConf
34
format
( )
2012 PHPConf
35
Input
var object = {
title: 'value1',
type: 'value2',
user_id: '1000'
};
Output
{
title: 'value1',
type: 'value2',
user_id: '1000',
success_text: 'ok'
}
http://site.com/API/Topic/Add
2012 PHPConf
36
Input
var object = {
title: 'value1',
type: 'value2'
};
Output
{
title: 'value1',
type: 'value2',
user_id: '1000',
success_text: 'ok'
}
http://site.com/API/Topic/Add/1000
2012 PHPConf
37
Input
var object = {
id: '1000',
title: 'value1',
type: 'value2'
};
Output
{
id: '1000',
title: 'value1',
type: 'value2',
success_text: 'ok'
}
http://site.com/API/Topic/Update
2012 PHPConf
38
Input
var object = {
title: 'value1',
type: 'value2'
};
Output
{
id: '1000',
title: 'value1',
type: 'value2',
success_text: 'ok'
}
http://site.com/API/Topic/Update/1000
2012 PHPConf
39
Input
var object = {
id: 1000
};
Output
{
id: '1000',
success_text: 'ok'
}
http://site.com/API/Topic/Delete
2012 PHPConf
40
Input
var object = {
id: [1000, 1001]
};
Output
{
id: '1000',
success_text: 'ok'
}
http://site.com/API/Topic/Delete
2012 PHPConf
41
Input
var object = {
Output
{
id: '1000',
success_text: 'ok'
};
}
http://site.com/API/Topic/Delete/1000
2012 PHPConf
42
Input
var object = {
id: 1000
};
Output
id: '1000',
success_text: 'ok',
item: {
title: 'Kate Upton'
}
}
http://site.com/API/Topic/List
2012 PHPConf
43
Input
var object = {
q: 'Kate Upton'
};
Output
id: '1000',
success_text: 'ok',
items: [
{title: 'I am kate'},
{title: 'I am Upton'}
]
}
http://site.com/API/Topic/List
2012 PHPConf
44
Kate
Upton
2012 PHPConf
45
Youtube API
2012 PHPConf
46
2012 PHPConf
47
API
2012 PHPConf
48
Old: http://site.com/v1/API/Topic/Add
New: http://site.com/v2/API/Topic/Add
2012 PHPConf
49
URI Routing
Framework or mod_rewrite
2012 PHPConf
50
http://site.com/API/Topic/Add
=
http://site.com/v1/API/Topic/Add
2012 PHPConf
51
API Implementation
2012 PHPConf
52
2012 PHPConf
53
Phil Sturgeons
CodeIgniter REST Server
http://github.com/philsturgeon/codeigniter-restserver
2012 PHPConf
54
Requirements
2012 PHPConf
55
How to install?
2012 PHPConf
56
Installation
application/libraries/Format.php
application/libraries/REST_Controller.php
application/config/rest.php
2012 PHPConf
57
$config['rest_default_format'] = 'json';
$config['rest_enable_keys'] = false;
$config['rest_enable_logging'] = false;
$config['rest_enable_limits'] = false;
$config['rest_ajax_only'] = false;
2012 PHPConf
58
2012 PHPConf
59
require(APPPATH.'/libraries/REST_Controller.php');
2012 PHPConf
60
Handling Requests
class Topic extends REST_Controller
{
public function index_get() {}
public function index_post() {}
public function index_update() {}
public function index_delete() {}
}
2012 PHPConf
61
CRUD Requests
class Topic extends REST_Controller
{
public function list_get() {}
public function add_post() {}
public function update_update() {}
public function delete_delete() {}
}
2012 PHPConf
62
2012 PHPConf
63
Parameters
GET
$this->get('blah');
POST
$this->post('blah');
UPDATE
$this->update('blah');
DELETE
$this->delete('blah');
2012 PHPConf
64
Create API
Input
var object = {
title: 'Kate Upton',
text: 'Beautiful girl'
};
Output
id: '1000',
success_text: 'ok',
http://site.com/API/Topic/Add
2012 PHPConf
65
66
Update API
Input
var object = {
id: 1000,
title: 'Kate Upton',
text: 'Beautiful girl'
};
Output
id: '1000',
success_text: 'ok',
http://site.com/API/Topic/Update
2012 PHPConf
67
68
Delete API
Input
var object = {
id: 1000
};
Output
id: '1000',
success_text: 'ok',
http://site.com/API/Topic/Delete
2012 PHPConf
69
70
Input
var object = {
id: 1000,
type: [1, 2]
};
Output
id: '1000',
success_text: 'ok',
item: {
title: 'Kate Upton'
}
}
http://site.com/API/Topic/List
2012 PHPConf
71
72
2012 PHPConf
73
Folder
application
controllers/
api/
topic.php
user.php
acl.php
system
index.php
2012 PHPConf
74
Routing (config/routes.php)
Default URL http://site.com/api/topic/Add
$route['API/Topic/(:any)'] = 'api/topic/$1';
$route['API/User/(:any)'] = 'api/user/$1';
$route['API/Acl/(:any)'] = 'api/acl/$1';
New URL http://site.com/API/Topic/Add
2012 PHPConf
75
76
2012 PHPConf
77
Phil Sturgeons
CodeIgniter REST Client
https://github.com/philsturgeon/codeigniter-restclient
2012 PHPConf
78
Requirements
2012 PHPConf
79
Requirements
PHP 5.1+
CodeIgniter 2.0.0+
CURL
2012 PHPConf
80
2012 PHPConf
81
Load Library
// Load the rest client spark
$this->load->spark('restclient/2.1.0');
// Load the library
$this->load->library('rest');
2012 PHPConf
82
2012 PHPConf
83
Parameter
// set api path
$api = '/API/Topic/Add';
// set api data
$data = array(
'title' => 'I am Kate Upton',
'type' => 'girl'
);
2012 PHPConf
84
Test it
// GET API
$this->rest->get($api, $data);
// POST API
$this->rest->post($api, $data);
// UPDATE API
$this->rest->update($api, $data);
// DELETE API
$this->rest->delete($api, $data);
2012 PHPConf
85
$this->rest->debug();
Rest Client Library debug mode
2012 PHPConf
86
2012 PHPConf
87
2012 PHPConf
88
2012 PHPConf
89
2012 PHPConf
90
2012 PHPConf
91
2012 PHPConf
92
RESTful API
2012 PHPConf
93
2012 PHPConf
94
CodeIgniter
2012 PHPConf
95
http://www.codeigniter.org.tw/forum/
2012 PHPConf
96
2012 PHPConf
97