Membuat Simple Service Menggunakan PHP ( Simple PHP Service App )
Disini kita akan membuat Simple Service Menggunakan PHP murni tanpa menggunakan framework. Tujuannya untuk:
2. Mampu membuat simple service menggunakan PHP
3. Mampu mengakses web service
Kodingan yang perlu kita buat & ketik :
1. File index.php
.<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
require __DIR__."/core/Bootstrap.php";
ini_set('display_errors', 1);
error_reporting(E_ALL);
require __DIR__."/core/Bootstrap.php";
2. File config/Routes.php
<?php
$routes = [
'/users' => 'Users.php',
'/' => 'Home.php',
];
?>
$routes = [
'/users' => 'Users.php',
'/' => 'Home.php',
];
?>
3. File controller/Home.php
<?php
$url = $_SERVER['REQUEST_URI'];
//if use folder path
$urlArr = explode("index.php", $url);
if (count($urlArr) >= 2) {
$url = $urlArr[1];
}
//if use php -s localhost:8000
if(strpos($url,"/")!== 0) {
$url = "/$url";
}
//untuk menghandle url /
if($url == '/' && $_SERVER ['REQUEST_METHOD'] == 'GET') {
echo json_encode(array('service_name' => 'PHP Service App', 'status' => 'running'));
}
$url = $_SERVER['REQUEST_URI'];
//if use folder path
$urlArr = explode("index.php", $url);
if (count($urlArr) >= 2) {
$url = $urlArr[1];
}
//if use php -s localhost:8000
if(strpos($url,"/")!== 0) {
$url = "/$url";
}
//untuk menghandle url /
if($url == '/' && $_SERVER ['REQUEST_METHOD'] == 'GET') {
echo json_encode(array('service_name' => 'PHP Service App', 'status' => 'running'));
}
4. File controller/User.php
<?php
$url = $_SERVER ['REQUEST_URI'];
//if use folder path
$urlArr = explode("index.php", $url);
if (count($urlArr) >= 2) {
$url = $urlArr[1];
}
//if use php -s localhost:8000
if(strpos($url,"/") !== 0){
$url = "/$url";
}
//untuk menghandle url /users
if($url == '/users' && $_SERVER ['REQUEST_METHOD'] == 'GET') {
$users = array(
array('id' => 1, 'name' => 'Sumatrana', 'email' => 'sumatrana@gmail.com', 'address' => 'Padang', 'gender' => 'Laki-Llaki'),
array('id' => 2, 'name' => 'Jawarianto', 'email' => 'jawarianto@gmail.com', 'address' => 'Cimahi', 'gender' => 'Laki-Llaki'),
array('id' => 3, 'name' => 'Kalimantio', 'email' => 'kalimantio@gmail.com', 'address' => 'Samarinda', 'gender' => 'Laki-Llaki'),
array('id' => 4, 'name' => 'Sulawesiani', 'email' => 'sulawesiani@gmail.com', 'address' => 'Makasar', 'gender' => 'Perempuan'),
array('id' => 5, 'name' => 'Papuani', 'email' => 'Papuani@gmail.com', 'address' => 'Jayapura', 'gender' => 'Perempuan'),
);
echo json_encode($users);
}
//untuk menghandle url /user/{ID}
if(preg_match("/users\/([0-9])+/", $url, $matches) && $_SERVER ['REQUEST_METHOD'] == 'GET'){
$users = array(
'1' =>array('id' => 1, 'name' => 'Sumatrana', 'email' => 'sumatrana@gmail.com', 'address' => 'Padang', 'gender' => 'Laki-Llaki'),
'2' => array('id' => 2, 'name' => 'Jawarianto', 'email' => 'jawarianto@gmail.com', 'address' => 'Cimahi', 'gender' => 'Laki-Llaki'),
'3' => array('id' => 3, 'name' => 'Kalimantio', 'email' => 'kalimantio@gmail.com', 'address' => 'Samarinda', 'gender' => 'Laki-Llaki'),
'4' => array('id' => 4, 'name' => 'Sulawesiani', 'email' => 'sulawesiani@gmail.com', 'address' => 'Makasar', 'gender' => 'Perempuan'),
'5' => array('id' => 5, 'name' => 'Papuani', 'email' => 'Papuani@gmail.com', 'address' => 'Jayapura', 'gender' => 'Perempuan'),
);
$user = $users[$matches[1]];
echo json_encode($user);
}
?>
$url = $_SERVER ['REQUEST_URI'];
//if use folder path
$urlArr = explode("index.php", $url);
if (count($urlArr) >= 2) {
$url = $urlArr[1];
}
//if use php -s localhost:8000
if(strpos($url,"/") !== 0){
$url = "/$url";
}
//untuk menghandle url /users
if($url == '/users' && $_SERVER ['REQUEST_METHOD'] == 'GET') {
$users = array(
array('id' => 1, 'name' => 'Sumatrana', 'email' => 'sumatrana@gmail.com', 'address' => 'Padang', 'gender' => 'Laki-Llaki'),
array('id' => 2, 'name' => 'Jawarianto', 'email' => 'jawarianto@gmail.com', 'address' => 'Cimahi', 'gender' => 'Laki-Llaki'),
array('id' => 3, 'name' => 'Kalimantio', 'email' => 'kalimantio@gmail.com', 'address' => 'Samarinda', 'gender' => 'Laki-Llaki'),
array('id' => 4, 'name' => 'Sulawesiani', 'email' => 'sulawesiani@gmail.com', 'address' => 'Makasar', 'gender' => 'Perempuan'),
array('id' => 5, 'name' => 'Papuani', 'email' => 'Papuani@gmail.com', 'address' => 'Jayapura', 'gender' => 'Perempuan'),
);
echo json_encode($users);
}
//untuk menghandle url /user/{ID}
if(preg_match("/users\/([0-9])+/", $url, $matches) && $_SERVER ['REQUEST_METHOD'] == 'GET'){
$users = array(
'1' =>array('id' => 1, 'name' => 'Sumatrana', 'email' => 'sumatrana@gmail.com', 'address' => 'Padang', 'gender' => 'Laki-Llaki'),
'2' => array('id' => 2, 'name' => 'Jawarianto', 'email' => 'jawarianto@gmail.com', 'address' => 'Cimahi', 'gender' => 'Laki-Llaki'),
'3' => array('id' => 3, 'name' => 'Kalimantio', 'email' => 'kalimantio@gmail.com', 'address' => 'Samarinda', 'gender' => 'Laki-Llaki'),
'4' => array('id' => 4, 'name' => 'Sulawesiani', 'email' => 'sulawesiani@gmail.com', 'address' => 'Makasar', 'gender' => 'Perempuan'),
'5' => array('id' => 5, 'name' => 'Papuani', 'email' => 'Papuani@gmail.com', 'address' => 'Jayapura', 'gender' => 'Perempuan'),
);
$user = $users[$matches[1]];
echo json_encode($user);
}
?>
5. File core/Bootstrap.php
<?php
require __DIR__.'/Router.php';
require __DIR__.'/../config/Routes.php';
$router = new Router;
$router->setRoutes($routes);
$url = $_SERVER ['REQUEST_URI'];
require __DIR__."/../controller/".$router->getFilename($url);
?>
require __DIR__.'/Router.php';
require __DIR__.'/../config/Routes.php';
$router = new Router;
$router->setRoutes($routes);
$url = $_SERVER ['REQUEST_URI'];
require __DIR__."/../controller/".$router->getFilename($url);
?>
6. File core/Router.php
<?php
class Router {
private $routes = [];
function setRoutes(Array $routes){
$this->routes = $routes;
}
function getFilename(string $url){
foreach($this->routes as $route => $file) {
if(strpos($url, $route) !== false) {
return $file;
}
}
}
}
?>
class Router {
private $routes = [];
function setRoutes(Array $routes){
$this->routes = $routes;
}
function getFilename(string $url){
foreach($this->routes as $route => $file) {
if(strpos($url, $route) !== false) {
return $file;
}
}
}
}
?>
Sesudah di buat file kodingnya dan di masukan/susun sesuai struktur folder seperti di atas, saatnya eksekusi.
Cara Menjalankan Aplikasi
Untuk menjalan aplikasi saya menggunakan cara :
1. Pada browser buka localhost/php-service-app/index.php , jika benar akan muncul seperti di bawah ini.
2. Selanjutnya saya menggunakan tools dalam mengakses API, tools nya adalah aplikasi "Postman"
3. Buka aplikasi Postman Pilih menu GET > masukan http://localhost/php-service-app/index.php >
Send.
4. Cek data users masukan http://localhost/php-service-app/index.php/users
0 Response to "Membuat Simple Service Menggunakan PHP ( Simple PHP Service App )"
Post a Comment