Beberapa waktu lalu seorang teman (Edwin) membuatkan webservice (SOAP) dengan menggunakan java (http://210.247.242.106/GISService/Service?wsdl) tentang database provinsi, kecamatan dan desa di Indonesia yang didapatkan dari Panji09 pada thread di kaskus.
Kali ini saya mencoba untuk membuat webservice (REST) sederhana dengan menggunakan Slim Framework.
Apa itu REST?
REST adalah Representational State Transfer.
- REST menggunakan protokol HTTP (Create, Read, Update, Delete) dengan metode POST, GET, PUT, DELETE.
- REST alternatif dari RPC atau webservice SOAP, WSDL dll.
- REST sebenarnya bukanlah sebuah standar, hanya sebuah Architectural style bukan seperti SOAP menggunakan protokol.
Kenapa Slim Framework?
- Karena mirip dengan Sinatra (Ruby).
- Slim menggunakan PHP >= 5.3.0.
- Ukurannya relatif kecil.
- Mudah dipelajari.
Fitur
Powerful router
Standard and custom HTTP methods
Route parameters with wildcards and conditions
Route redirect, halt, and pass
Route middleware
Template rendering with custom views
Flash messages
Secure cookies with AES-256 encryption
HTTP caching
Logging with custom log writers
Error handling and debugging
Middleware and hook architecture
Simple configuration
Contoh penggunaan slim dengan koneksi ke database lewat PDO :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
require 'Slim/Slim.php';
require 'db.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim(array(
'mode' => 'production',
'log.writer' => new \Slim\Extras\Log\DateTimeFileWriter(array(
'path' => './logs',
'name_format' => 'Y-m-d',
'message_format' => '%label% - %date% - %message%'
))
));
$app->contentType("application/json");
$app->get('/getProvinsis', 'getProvinsis');
$app->get('/getKabupatens/:id', 'getKabupatens');
$app->get('/getKecamatans/:id', 'getKecamatans');
$app->get('/getDesas/:id', 'getDesas');
// Only invoked if mode is "production"
$app->configureMode('production', function () use ($app) {
$app->config(array(
'log.enable' => true,
'log.level' => \Slim\Log::WARN,
'debug' => false
));
});
// Only invoked if mode is "development"
$app->configureMode('development', function () use ($app) {
$app->config(array(
'log.enable' => false,
'log.level' => \Slim\Log::DEBUG,
'debug' => true
));
});
$app->notFound(function () use ($app) {
echo 'notFound coy';
});
$app->run();
//All Functions goes here
function getProvinsis()
{
global $app;
$sql = "select * FROM provinsi ORDER BY id";
try {
$app->etag('getProvinsis');
$db = getConnection();
$stmt = $db->query($sql);
$provinsis = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
echo json_encode($provinsis);
} catch(\PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
function getKabupatens($id)
{
$sql = "SELECT * FROM kabupaten WHERE id_prov = :id";
try {
$db = getConnection();
$stmt = $db->prepare($sql);
$stmt->bindParam("id", $id);
$stmt->execute();
$kabupatens = $stmt->fetchAll();
$db = null;
echo json_encode($kabupatens);
} catch (\PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
function getKecamatans($id)
{
$sql = "SELECT * FROM kecamatan WHERE id_kabupaten = :id";
try {
$db = getConnection();
$stmt = $db->prepare($sql);
$stmt->bindParam("id", $id);
$stmt->execute();
$kecamatans = $stmt->fetchAll();
$db = null;
echo json_encode($kecamatans);
} catch (\PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
function getDesas($id)
{
$sql = "SELECT * FROM desa WHERE id_kecamatan = :id";
try {
$db = getConnection();
$stmt = $db->prepare($sql);
$stmt->bindParam("id", $id);
$stmt->execute();
$desas = $stmt->fetchAll();
$db = null;
echo json_encode($desas);
} catch (\PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
Download
Anda dapat mendownload source code di https://github.com/jefrip/GISService
Referensi :
- https://github.com/edwinkun/GISService
- http://coenraets.org/blog/2011/12/restful-services-with-jquery-php-and-the-slim-framework/